Control array in VB.NET
Controls in .NET are just normal objects so you can freely put them into normal arrays or lists. The special VB6 construct of control arrays is no longer necessary.
So you can for example say,
Dim buttons As Button() = { Button1, Button2, … }
For Each button As Button In Buttons
button.Text = "foo"
End For
Alternatively, you can directly iterate over the controls inside a container (e.g. a form):
For Each c As Control In MyForm.Controls
Dim btt As Button = TryCast(c, Button)
If btt IsNot Nothing Then ' We got a button!
btt.Text = "foo"
End If
End For
Notice that this only works for controls that are directly on the form; controls nested into containers will not be iterated this way; you can however use a recursive function to iterate over all controls.
You create a Form and add a Layout 10 * 10, and try this,
Public Class Form1
Private NRow As Integer = 10
Private NCol As Integer = 10
Private BtnArray(NRow * NCol - 1) As Button
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TableLayoutPanel1.Size = Me.ClientSize
For i As Integer = 0 To BtnArray.Length - 1
BtnArray(i) = New Button()
BtnArray(i).Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
BtnArray(i).Text = CStr(i)
TableLayoutPanel1.Controls.Add(BtnArray(i), i Mod NCol, i \ NCol)
AddHandler BtnArray(i).Click, AddressOf ClickHandler
Next
End Sub
Public Sub ClickHandler(ByVal sender As Object, ByVal e As System.EventArgs)
MsgBox("I am button #" & CType(sender, Button).Text)
End Sub
End Class
You can't create a control array in VB.NET, but you can archive similar functionality using the Handles
keyword.
public sub Button_Click(sender as Object, e as EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
'Do Something
End Sub
Yes, you can do this. But I don't think you can iterate buttons directly by giving myForm.