0

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.

4
  • You should use a List(Of Button). Commented Dec 23, 2014 at 18:40
  • 1
    Neither an array or List will do much to help. VB.NET is not like VB6. If what you want is 49 buttons that all do the same thing, use a common event handler. Commented Dec 23, 2014 at 18:43
  • The common Event handler is what I started to do at the very beginning. Correct? Commented Dec 23, 2014 at 18:54
  • yes, with the multiple Handles clauses. you can also do that dynamically at runtime using AddHandler Commented Dec 23, 2014 at 19:31

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

I just used your code and got an error. The error is: 'Controls' is not declared Not sure if it matters but I am using Visual Studio 2013 Express?
@DuckyDarkling Controls is a property of the form. If you are executing this outside the form you'll have to properly pass your form.
I did minor modification to your code and it works now. I am now able to get my index from the button that is clicked and then modify that button and others through the list. Thanks a ton.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.