Resolved Multiple Controls trigger one event?

jcardana

Old to VB6, New to VB.NET
Joined
Oct 26, 2015
Messages
72
Location
Rio Rancho, NM
Programming Experience
Beginner
I'm back, trying to figure out VB.net. Nothing I'm getting from Google is helping me understand.

Without Control Arrays, I'm stuck on how to have 55 textboxes' Change Events trigger just one Change Event so I can know the files been changed.

Thank you for your time and efforts.
 
I don't know what it is that makes me find the solution after I post here
 
What you're talking about is handling the 55 different events with one method. The answer is the Handles clause of the event handler method.

When you double-click a control in the designer, it will generate a method to handle the default event, e.g. the TextChanged event of a TextBox. If you double-click a control named TextBox1, you'll see this code generated:
VB.NET:
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

End Sub
Note the Handles clause there. That specifies which events are handled by that method. You can manually edit that Handles clause if you want, e.g.
VB.NET:
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged

End Sub
but I wouldn't recommend that for 55 controls. The way you should normally do it is first select all the controls in the designer, then open the Properties window and click the Events button. That will list all the events for the selected item(s) and you can then double-click an event to generate an event handler. If you have selected multiple controls, they will all be included in the Handles clause. You can also use the drop-down for any event to select an existing event handler and have that event for that control added to the Handles clause. I would suggest that, if you use a single method to handle multiple events, you change the name to reflect that, e.g. TextBoxes_TextChanged instead of TextBox1_TextChanged.
 
I don't know what it is that makes me find the solution after I post here

It's something like talking to a therapist. Much of the time, the therapist doesn't actually do anything but give you someone to talk to and hearing yourself say the words helps you work out what to do about the problem. Often times, describing a problem in a forum post or the like actually clarifies the issue in your mind and makes you better able to find a solution for yourself, e.g. with better search keywords.
 
Thank you for clarifying my terminology... I have so much to learn. And thank you for the steps on creating the Method by clicking the Events button in the Properties. That saves a lot of copy/paste/edit ;)
 
Back
Top