locked
disable text change event

    Question

  • i am having a text change event,

    i am writing a function to clear the values, while doing so the text change event is triggered and unwanted things happen, i need this event to happen when the text edited manually and not while clearing the text through the function

    i need that event not to be triggered while clearing the values of the textboxes,
     is there any options in VB.net to disable that event for temporary and then enabling the functionality

    Thanks in advance

    URGENT!!!
    Wednesday, January 31, 2007 9:07 AM

Answers

  • Use a boolean to suspend the change when you're clearing the text through code:


    Private zDoNotDoEvent as Boolean = false

    Event Sub TextChanged()
    If zDoNotDoEvent = True then exit sub

    ' eventstuff that would normally happen
    End Sub


    Sub TextChangeViaCode()
    zDoNotDoEvent = True

    'Clear Text

    zDoNotDoEvent =False
    End Sub


    You could also use AddHandler/RemoveHandler for the event, but the boolean works just as good.
    Wednesday, January 31, 2007 9:32 AM
  • But i currently use
    private sub Clear()
        RemoveHandler txtFirst.TextChanged, AddressOf Inputs_TextChanged
        txtFirst.text = ""
        AddHandler txtFirst.TextChanged, AddressOf Inputs_TextChanged
    End Sub
    is this ok too? but sometimes it still seem to trigger the text changed event
    Wednesday, January 31, 2007 11:00 AM

All replies

  • Use a boolean to suspend the change when you're clearing the text through code:


    Private zDoNotDoEvent as Boolean = false

    Event Sub TextChanged()
    If zDoNotDoEvent = True then exit sub

    ' eventstuff that would normally happen
    End Sub


    Sub TextChangeViaCode()
    zDoNotDoEvent = True

    'Clear Text

    zDoNotDoEvent =False
    End Sub


    You could also use AddHandler/RemoveHandler for the event, but the boolean works just as good.
    Wednesday, January 31, 2007 9:32 AM
  • But i currently use
    private sub Clear()
        RemoveHandler txtFirst.TextChanged, AddressOf Inputs_TextChanged
        txtFirst.text = ""
        AddHandler txtFirst.TextChanged, AddressOf Inputs_TextChanged
    End Sub
    is this ok too? but sometimes it still seem to trigger the text changed event
    Wednesday, January 31, 2007 11:00 AM
  • You'll have to wait for someone with more insight than me to come along, IceAngel89, as I would expect the  AddHandler/RemoveHandler method to work
    Wednesday, January 31, 2007 11:08 AM
  • in a kind this is used as semaphore??

    the same as we do using boolean..
    Thanks (both of you)
    Wednesday, January 31, 2007 11:10 AM
  • Both ways can be used - the boolan flag is like a semaphore, if that's what you are asking.

    Remember to put the addhandler back after removing the handler - which comes down to the crux of the matter: it's easy to forget and troubleshoot as the code starts to become more complex (events fire when you think they shouldn't and they don't fire when you think they should).

    This is why the 'flag' method is preferable (in almost all circumstances, but not always): it makes it easier to troubleshoot. Since the event always fires when it should, you can prevent any activity in the event by checking the flag. You can add a breakpoint in the event knowing it will always break at that point, regardless as to whether the flag is set or not.

    Wednesday, January 31, 2007 4:20 PM
    Moderator