I'm sorry to tell you this, but control arrays have left the building.
Disappointing, isn't it?
If you want a bunch of buttons to perform the same event, just add your
own
event handlers for them in your Form_Load event:
AddHandler myButton1.Click, DoThisForAllOfThem
AddHandler myButton2.Click, DoThisForAllOfThem
AddHandler myButton3.Click, DoThisForAllOfThem
AddHandler myButton4.Click, DoThisForAllOfThem
AddHandler myButton5.Click, DoThisForAllOfThem
If you want to know which button invoked the click, you can probably do
it
some fancy way (Reflection comes to mind), but I just put some
identifier
in the [Tag] property for each button and check that like this:
Private Sub DoThisForAllOfThem(ByVal sender as Object, ByVal e as
EventArgs)
Dim theButton As Button = DirectCast(sender, Button)
Select Case theButton.Tag
Case "1"
GoToSchool
Case "2"
GoToWork
Case "3"
GoShopping
Case "4"
GoToDinner
Case "5"
GoDrinking
End Select
End Sub
Be sure your buttons have a tag in them, if it is blank, this will throw
a
NullException.
While the Tag property is useful, you can also just check the button
itself:
Private Sub DoThisForAllOfThem(ByVal sender as Object, ByVal e as
EventArgs)
Select Case True
Case sender is myButton1
GoToSchool
Case sender is myButton2
GoToWork
Case sender is myButton3
GoShopping
Case sender is myButton4
GoToDinner
Case sender is myButton5
GoDrinking
End Select
End Sub
Although some don't like using the Select Case True trick.
Another option would be to use the Tag property and store a delegate
to the desired function there and then just call the function.
Public Delegate Sub DoSomethingDelegate() 'This delegate sig
matches the desired sub
Public Sub New()
InitializeComponent
Button1.Tag = New DoSomethingDelegate(AddressOf GoToSchool)
Button2.Tag = New DoSomethingDelegate(AddressOf GoToWork)
Button3.Tag = New DoSomethingDelegate(AddressOf GoOutDrinking)
'etc.
End Sub
Private Sub DoThisForAllOfThem(ByVal sender as Object, ByVal e as
EventArgs)
Dim theButton As Button = DirectCast(sender, Button)
DirectCast(theButton.Tag, DoSomethingDelegate).Invoke()
End Sub
Public Sub GoToSchool()
End Sub
Public Sub GoShopping()
End Sub
'etc.
This method is more complicated and perhaps a little less readable so
I don't know if it is very useful, but you know the old saying:
"There's more than one way to skin a cat" (By the way, who comes up
with these sayings anyway? This one is kind of sick!!)
Chris