Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • Leaders
Posted

For situations like this in the past, I've written code like the following:

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

[sIGPIC]e[/sIGPIC]
Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...