Get the name of controls (buttons) dynamically
controls, vb.net, winforms
Solution
You should not need to do it in this error-prone way just to save you some lines of code. But if you really want....
You can use a `Panel` or another container control that groups the related controls logically. Then use `MyPanel.Controls.OfType(Of Button)()` to filter and find all the buttons there.
For Each btn As Button In MyPanel.Controls.OfType(Of Button)()
btn.Text = "blah" 'from database...or something
Next
Another way is to put them all in an array or other collection type like `List(Of Button)` first and loop over them afterwards:
Dim myButtons = {button1, button2, button3, button4, button5, button6}
For Each btn In myButtons
btn.Text = "blah" 'from database...or something
Next
Last you could use `ControlCollection.Find` to find controls with a given string for its name:
For i As Int32 = 1 To 10
Dim btns = Me.Controls.Find("button" & i, True)
If btns.Length > 0 Then
btns(0).Text = "blah" 'from database...or something
End If
Next
Problem
I have 10 buttons namely `button01`, `button02` ... `button10`. What I want is how to manipulate it. ``` For x=1 to 10 button(x).text = "blah" 'from database...or something next ``` I need to do this because I have 10 buttons or more and I want to manipulate it through initialization. So that I don't do it manually one by one. I don't know how to do this. I'm still new in .NET.