C expressed precisely :
In VB6 I could click an array of labels with Label1_Click (Index). How
do I do this in VB.net?
In VB.net, I created an array of labels by Dim Label1 as Label, and
then Label1(1) = Label101, Label1(2) = Label102, etc.
I can write Label101_Click, but it would be too messy to copy the code
to 32 labels.
Thanks.
A single event handler can handle multiple controls. Assuming you have
VS2008
1. In the designer, select all of the lables you want to be assigned
to the event.
2. In the properties window, select the little lighting bolt icon.
This will show the events of the controls. In the event you want to
share, type the name of the procedure - such as LabelClick. Hit Enter.
VS will create a sub that looks something like this (I did mine with
buttons - but it works the same for all controls, in fact, as long as
the event shares the same signature - they don't even have to be the
same type of control):
Private Sub ButtonClick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click, Button2.Click, Button1.Click
End Sub
Now, if any of the 3 buttons are clicked, this sub will be called. You
can differentiate which control was clicked using the sender argument.
Private Sub ButtonClick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click, Button2.Click, Button1.Click
MessageBox.Show(DirectCast(sender, Control).Text)
End Sub
And here's another example using different types of controls:
Private Sub ButtonClick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click, Label1.Click
MessageBox.Show(DirectCast(sender, Control).Text)
End Sub
..NET events are much more powerful then VB6 control arrays once they
are understood.