none
Texbox.TextChanged RRS feed

  • Question

  • I have two textboxes "TextBox1" and "TextBox2" and both of them fire a TextChanged event. TextBox1 should always be 2 less than TextBox2.

    So if the user type 10 in TextBox1, TextBox2 would be 12,

    if the user type 10 in TextBox2, TextBox1 would be 8.

    How do I achieve that without having the issue of circular reference?

    Thanks for all your help.


    Wednesday, July 17, 2019 9:54 PM

Answers

  • Hi

    Give this event handler a try out.

    	Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
    		Dim tb As TextBox = DirectCast(sender, TextBox)
    		Dim temp As Integer = 0
    		Integer.TryParse(tb.Text, temp)
    		Select Case tb.Name
    			Case "TextBox1"
    				RemoveHandler TextBox2.TextChanged, AddressOf TextBox_TextChanged
    				TextBox2.Text = (temp + 2).ToString
    				AddHandler TextBox2.TextChanged, AddressOf TextBox_TextChanged
    			Case "TextBox2"
    				RemoveHandler TextBox1.TextChanged, AddressOf TextBox_TextChanged
    				TextBox1.Text = (temp - 2).ToString
    				AddHandler TextBox1.TextChanged, AddressOf TextBox_TextChanged
    		End Select
    	End Sub


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, July 18, 2019 12:57 PM
    • Marked as answer by Solarch Thursday, July 18, 2019 2:06 PM
    Wednesday, July 17, 2019 11:49 PM
  • Hi,

    What is  circular reference?

    Public Class Form1
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            TextBox2.Text = (CInt(TextBox1.Text) + 2).ToString()
        End Sub
    
        Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
            TextBox1.Text = (CInt(TextBox2.Text) - 2).ToString()
        End Sub
    
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, July 18, 2019 4:17 AM
    Moderator

All replies

  • Hi

    Give this event handler a try out.

    	Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
    		Dim tb As TextBox = DirectCast(sender, TextBox)
    		Dim temp As Integer = 0
    		Integer.TryParse(tb.Text, temp)
    		Select Case tb.Name
    			Case "TextBox1"
    				RemoveHandler TextBox2.TextChanged, AddressOf TextBox_TextChanged
    				TextBox2.Text = (temp + 2).ToString
    				AddHandler TextBox2.TextChanged, AddressOf TextBox_TextChanged
    			Case "TextBox2"
    				RemoveHandler TextBox1.TextChanged, AddressOf TextBox_TextChanged
    				TextBox1.Text = (temp - 2).ToString
    				AddHandler TextBox1.TextChanged, AddressOf TextBox_TextChanged
    		End Select
    	End Sub


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, July 18, 2019 12:57 PM
    • Marked as answer by Solarch Thursday, July 18, 2019 2:06 PM
    Wednesday, July 17, 2019 11:49 PM
  • Hi,

    What is  circular reference?

    Public Class Form1
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            TextBox2.Text = (CInt(TextBox1.Text) + 2).ToString()
        End Sub
    
        Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
            TextBox1.Text = (CInt(TextBox2.Text) - 2).ToString()
        End Sub
    
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, July 18, 2019 4:17 AM
    Moderator
  • Hi,
    you can use data binding. In this case you can prevent circular reference. try this demo:

    Public Class Form1
    
      Private tb1 As New TextBox With {.Dock = DockStyle.Top}
      Private tb2 As New TextBox With {.Dock = DockStyle.Top}
    
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Controls.AddRange(New Control() {tb2, tb1})
        tb1.DataBindings.Add("Text", Me, "Prop1")
        tb2.DataBindings.Add("Text", Me, "Prop2")
      End Sub
    
      Private _prop As Integer = 0
      Public Property Prop1 As Integer
        Get
          Return Me._prop
        End Get
        Set(value As Integer)
          Me._prop = value
        End Set
      End Property
      Public Property Prop2 As Integer
        Get
          Return Me._prop + 2
        End Get
        Set(value As Integer)
          Me._prop = value - 2
        End Set
      End Property
    
    End Class


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Thursday, July 18, 2019 5:34 AM
  • Thank you for your solution, yes, it works great. Even if my question did not illustrate the circular issue very well, your answer addresses the issue.  For example if we want to add 3 to textbox1 when textbox2 is changed and subtract 2 to textbox2 when textbox1 is changed, then we will face an endless chain of TextChanged events. I am writing this not to mislead others by my original question for If textbox1 is textbox2+2 and textbox1 = textbox2-2 we would not face the problem and the solution given by Alex would also work in that case.

    Thanks again for your help.

    Sol


    • Edited by Solarch Thursday, July 18, 2019 3:37 PM
    Thursday, July 18, 2019 3:36 PM
  • Alex, 

    Thanks for your answer. Yes, it answers the question I asked. But, I had not stated my question very well. Actually what I wanted was for TextBox1 to be TextBox2 + 2 when TextBox2 is changed and TextBox2 to TextBox2 - 3 when TextBox1 is changed. In that case we will face endless chain of events. But for the question as I stated your solution would work great for we would not have the circular reference problem.

    Thanks again for your help.

    Thursday, July 18, 2019 3:42 PM