Ducky Darkling
Ducky Darkling

Reputation: 5

create array of existing buttons vb 2013

I used to program in VB6 and am trying to write the same program in VB 2013. In this program I use an array of 49 buttons that all do the same thing when you click on them. I have figured out have to do the click function to a point:

Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button9.Click, Button10.Click, Button11.Click, Button12.Click, Button13.Click, Button16.Click, Button17.Click, Button18.Click, Button19.Click, Button20.Click
   ...

End Sub

What I am trying to do is simplify the code down to using an array so I can just pass on the index. One other person asked the same question in 2010 and the best answer was:

Button[] array = { firstButton, secondButton };

That would work but I want something with less typing. I also tried the following with failure:

One

Button[] buttons = this.Controls.OfType<Button>().ToArray();

Two

For i = 1 To 100
  Dim btns() As Button = Controls.Find("Button" & i, True)
  Dim btn As Button
  If btns IsNot Nothing Then
    btn = btns(0)
    'If buttons Is Nothing Then
    '  ReDim buttons(0)
    'Else
    '  ReDim Preserve buttons(buttons.Length)
    'End If
    'buttons(UBound(buttons)) = btn
    btn.Text = i - 1 'here you can change whatever you want
  End If
Next

Three

Dim buttons() As Button

buttons = Nothing
For Each b As Button In Me.Controls
If buttons Is Nothing Then
    ReDim buttons(0)
Else
    ReDim Preserve buttons(buttons.Length)
End If
buttons(UBound(buttons)) = b
Next

I just can't get it to accept the existing buttons into an array. I hope someone can help.

Upvotes: 0

Views: 2320

Answers (2)

Mark
Mark

Reputation: 8160

If your Buttons are nested inside container controls (e.g. a GroupBox) then you will need to perform a recursive search for all buttons. Maybe something like this (totally unoptimized)...

Private Function FindAllButtons(root As Control) As List(Of Button)
    Dim result As List(Of Button) = New List(Of Button)()
    For Each c As Control In root.Controls
        If TypeOf c Is Button Then
            result.Add(DirectCast(c, Button))
        ElseIf c.HasChildren Then
            result.AddRange(FindAllButtons(c))
        End If
    Next
    Return result
End Function

Then just call that in your Form:

Dim allButtons as List(Of Button) = FindAllButtons(Me)
' Add common Click handler
For Each b As Button In allButtons
    AddHandler b.Click, AddressOf Button_Click
Next

Update Just for fun, here's a generic version to find other types of control.

Private Function FindAllControls(Of T As Control)(root As Control) As List(Of T)
    Dim result As List(Of T) = New List(Of T)()
    For Each c As Control In root.Controls
        If TypeOf c Is T Then
            result.Add(DirectCast(c, T))
        ElseIf c.HasChildren Then
            result.AddRange(FindAllControls(Of T)(c))
        End If
    Next
    Return result
End Function

You can use that like:

Dim allButtons As List(Of Button) = FindAllControls(Of Button)(Me)
Dim allTextBoxes As List(Of TextBox) = FindAllControls(Of TextBox)(Me)

Upvotes: 1

the_lotus
the_lotus

Reputation: 12748

Option two will work, you just need to add the button found into a list. I suggest you add your buttons into a List(Of ) instead of an array. You can always convert the list into an array if you really need to.

    Dim buttonList As New List(Of Button)

    For i As Integer = 1 To 100
        Dim btns() As Control = Controls.Find("Button" & i, True)

        If btns IsNot Nothing AndAlso btns.Length > 0 Then
            buttonList.Add(CType(btns(0), Button))
        End If
    Next

Upvotes: 0

Related Questions