none
Textbox TextChanged Event

    Question

  • Hi,

    How can I bypass the TextChanged event if I populate the textbox during loading?

    for example

    When the VB Win form application starts it loads several text boxes with default information.
    thus firing the TextChanged event. (which I don't want to happen)

    I DO want to record any changes made by a user when he/she edits a textbox contents.

    Friday, July 30, 2010 4:49 PM

Answers

  • That makes sense. so..

    dim IsStartingUp as Boolean = True

    After loading all textboxes ...
    IsStartingUp = False

    Then in the TextChanged event...

    If IsStartingUp Then
    Exit Sub
    Else
    TextValue = Me.TextBox1.Text
    EndIf

     


    That would work but it'd be simpler to do something like:

     

    If IsStartingUp=False Then
         ' Put your routine here
    End If

     

    That way if it's true, then it will obviously exit the sub.

    • Marked as answer by jacobsdesigns Friday, July 30, 2010 9:54 PM
    Friday, July 30, 2010 5:30 PM

All replies

  • What I've come to do is to place a class-level boolean that I usually call something like "IsStartingUp" and set it initially to "True". Then at the last line of your form's load event, set it to "False".

    Now in the TextChanged event, qualify that IsStartingUp is False using an If/Then.

    Does that make sense?

    • Proposed as answer by Blackwood Friday, July 30, 2010 5:22 PM
    Friday, July 30, 2010 5:02 PM
  • Frank's answer is simple and works well. Another possibility is to remove the "Handles" clause from your event handler and wait until you have finished initialising the TextBox before setting up the event handler with the AddHandler statement.

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    	TextBox1.Text = "Initial value"
    	AddHandler TextBox1.TextChanged, AddressOf TextBox1_TextChanged
    End Sub
    
    Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    	'Handle user input
    End Sub
    
    

    Friday, July 30, 2010 5:25 PM
  • That makes sense. so..

    dim IsStartingUp as Boolean = True

    After loading all textboxes ...
    IsStartingUp = False

    Then in the TextChanged event...

    If IsStartingUp Then
    Exit Sub
    Else
    TextValue = Me.TextBox1.Text
    EndIf

     

    Friday, July 30, 2010 5:27 PM
  • That makes sense. so..

    dim IsStartingUp as Boolean = True

    After loading all textboxes ...
    IsStartingUp = False

    Then in the TextChanged event...

    If IsStartingUp Then
    Exit Sub
    Else
    TextValue = Me.TextBox1.Text
    EndIf

     


    That would work but it'd be simpler to do something like:

     

    If IsStartingUp=False Then
         ' Put your routine here
    End If

     

    That way if it's true, then it will obviously exit the sub.

    • Marked as answer by jacobsdesigns Friday, July 30, 2010 9:54 PM
    Friday, July 30, 2010 5:30 PM
  • adding the handler later is probably a better idea, sipmly because this is the type of event that is called quite a lot, so adding extra code to it is something to be avoided.  If it was an event less frequently called then it's not so much of an issue.
    Friday, July 30, 2010 6:13 PM
  • Having done both ways, setting the flag bit is a more reliable way to go.

    While removing and adding the handler may seem 'cooler', unless you are an extremely diligent programmer who comments things to the n-th degree( and that guy died around 1995), it's got the potential to introduce bugs at the benefit of sleeker code.

    So, the start of each routine which needs 'bypassing' you can have (well, what *I *may have, anyway):

    If Not Initialized Then Return

    The 'flag' method is semi-self documenting, also. While it's very 'old school', but YMMV.

    I won't get into an argument about code performance - had that argument before when someone pointed out that the above one line of code executes each time the method is called (whoda thunk it?) and then pointed out the thousand lines of code behind a simple database connect and call.

    The bottom line is to produce bug free code: if that takes a few extra lines of code then any performance hit is trivial. To that end, both methods can work (manually adding the handlers, or using a flag).

     


    Stephen J Whiteley
    Friday, July 30, 2010 8:56 PM
    Moderator