Control array in VB.NET
Asked Answered
F

3

0

How do I make a control array for buttons in VB.NET? Like in Visual Basic 6.0...

Is it possible that the syntax can be like the following?

 dim a as button

 for each a as button in myForm
   a.text = "hello"
 next
Fennel answered 31/3, 2011 at 8:58 Comment(1)
See also #40041 and https://mcmap.net/q/1164011/-vb6-control-arrays-in-netLeslie
L
2

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.

Leavings answered 31/3, 2011 at 9:3 Comment(3)
Control Array in VB is different I thinkGigi
@vsdev It’s only different because of necessity: VB6 didn’t allow putting controls into ordinary arrays.Leavings
just additional question sir.. :) in button collections, how to detect if what button did i clicked..Fennel
G
2

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.

Gigi answered 31/3, 2011 at 9:1 Comment(0)
L
2

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.

Leavings answered 31/3, 2011 at 9:3 Comment(3)
Control Array in VB is different I thinkGigi
@vsdev It’s only different because of necessity: VB6 didn’t allow putting controls into ordinary arrays.Leavings
just additional question sir.. :) in button collections, how to detect if what button did i clicked..Fennel
P
1

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
Pomeroy answered 31/3, 2011 at 12:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.