Well - since you posted code I'm thinking you are not asking people to do your homework for you so...
The trick is to add the index of each button into the Tag property of each button, and then add your own eventhandler for all of the buttons in the array. There will be one event handler for all of your buttons. Do that like this:
-
For I = 0 to 4
-
'put the index into the tag property
-
Choice(I).Tag = I.ToString
-
AddHandler Choice(I).Click, AddressOf MyChoice_Click
-
Next I
-
Now you create a sub called MyChoice_Click
-
Private Sub MyChoice_Click(Sender as Object, e as Eventargs)
-
Select Case CInt(Sender.Tag)
-
Case 0
-
'Do This
-
Case 1
-
'Do This
-
Case 2
-
'Do This
-
Case 3
-
'Do This
-
Case 4
-
'Do This
-
Case Else
-
'handle the error
-
End Select
-
-
End Sub
-
Because the Tag property of each button in the array carries the objects index in the array (because you assigned it with
Choice(I).Tag = I.ToString), that tag is now easily readable in the event you set up with the event handler. This allows you to know which radio button was selected and act accordingly.
What you needed to know is that each button you instantiated already had its own click event - so you could assign that to your event handler. Next you needed to know that the signature of the event you created (
MyChoice_Click) used the common
(Sender as object, e as EventArgs) - and the
Sender as Object will contain the Tag property of the object that called the event handler. The Intellisense in VS does not show you the tag property, you have to know its there - and it is because the base Object class in .net contains the Tag property and the Radiobutton is derived from that class.
Hope this helps.
By the way - here's my code for the whole thing:
-
Public Class Form1
-
Private MyButton(4) As RadioButton
-
-
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
-
Dim iTop As Integer = 0
-
For I = 0 To 4
-
iTop += 30
-
MyButton(I) = New RadioButton With {.Top = iTop, .Left = 25, .Parent = Me, .Tag = I.ToString}
-
Me.Controls.Add(MyButton(I))
-
MyButton(I).Show()
-
AddHandler MyButton(I).Click, AddressOf MyButton_Click
-
Next
-
End Sub
-
-
Private Sub MyButton_Click(Sender As Object, e As EventArgs)
-
Select Case CInt(Sender.tag)
-
Case 0
-
'Do this
-
Case 1
-
'Do this
-
Case 2
-
'Do this
-
Case 3
-
'Do this
-
Case 4
-
'Do this
-
Case Else
-
'handle the error
-
End Select
-
End Sub
-
End Class
-