Events Called More than one

ehelin

Freshman
Joined
Oct 11, 2005
Messages
48
Hi:

I am using a combo box and validating its contents as the text changes. At the start of the method, I remove the handler (to prevent multiple calls) and then add the handler back when I am done. However, the event is called 2 more times (3 total) after the event exits. I have noticed this with the key events as well. In my app, I am not trapping any other events...what is causing the same event to be called 3 times?

Any thoughts would be appreciated.

Thnks!

Eric

The event code is as follows:

Private Sub cbExamRoom_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
RemoveHandler cbExamRoom.TextChanged, AddressOf cbExamRoom_TextChanged

Try
If VarInitialLoadComplete Then
AutoComplete(cbExamRoom)
End If

Catch ex As Exception
LocalErrorLink.Error_Handler(ex)
Finally
AddHandler cbExamRoom.TextChanged, AddressOf cbExamRoom_TextChanged
End Try

End Sub
 
Is there a reason why you are removing / adding the handler within the event itself? Have you encountered problems with the event being triggered multiple times? Also where is the event handler being attached in the first place?
 
For situations like this in the past, I've written code like the following:
Visual Basic:
Boolean ValidatingTextBox1 = False;
 
Sub TextBox1_TextChanged(Sender As Object, e As EventArgs){
    'If we change the text in this sub the event code won't be
    're-executed...
    If Not ValidatingTextBox1 Then 
        'because we set this to true while validating
        ValidatingTextBox1 = True;
        'Perform Validation
        ValidatingTextBox1 = False;
    End If
End Sub
 
Events

Hi:

First, thanks for the responses! The first question(s)...

1) Removing/adding the handlers in the event was a co-workers idea...the thought was that it would remove the need for having a boolean class variable (like the other poster suggested)...with a bunch of textboxes...each with the same types of validations...this led to about 20+ class variables in my main form as well as repeated event handlers which essentially did the same thing, which with all the other things going on, I thought this was needlessly complex.
2) Yes...not sure why, but the events (when they fire multiple times...even with out trapping them) seem to fire in trios.
3) The event handlers are added at the end of the form load.

After much consideration, I opted to subclass the microsoft text box control and add all of the validations here (again, shared by all of the different txt boxes). It achieves the original goal of making the main form code less confusing and it also reduces the code duplications while retaining the required validations.

Thanks!

Eric
 
Back
Top