-1

I'm working in VB2010 and I think I can create a button array in code; however, I'm struggling to then refer to the created buttons individually to code their click events so that they work at runtime.

Any help would be greatly appreciated. I'm fairly new to vb programmimg so go easy on me!!

user2062689
  • 9
  • 1
  • 2

2 Answers2

1

Give this a try:

' However many buttons you want
Dim numButtons As Integer = 5
Dim ButtonArray(numButtons) as Button
Dim i As Integer = 0
For Each b As Button in ButtonArray
   b = new button
   AddHandler b.Click, AddressOf Me.ButtonsClick
   b.Tag = "b" & i
   ' You can also set things like button text in here.
   i += 1
Next


Private Sub ButtonsClick(sender As Object, e As System.EventArgs)
    ' sender is the button that has been clicked. You can
    ' do what you'd like with it, including cast it as a Button.
    Dim currButton As Button = CType (sender, Button)
    Select Case currButton.Tag
        Case "b0":
          ' This is the first button in the array. Do things!
        Case "b1":
          ' This is the second button in the array. Do things!
        Case "b2":
          ' Notice a pattern?

        '...

    End Select
End Sub
Chris Stauffer
  • 352
  • 1
  • 8
  • +1 but you'll get more votes if you show him the second method on how to describe a button click event that `Handles Button1.Click, Button2.Click` – Jeremy Thompson Feb 11 '13 at 22:25
0

On one of the button click events insert something like , button2.click which will perform the same action.

Or you may need to look into addHandler.....