[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

Array of control

I used to use array of controls in VB6 such as image control to share the same code between them and use index of the array to differentiate between them. Can I do the same in VB.NET?
1 Solution
VB.NET  no longer supports control arrays(control collection that share the same name).
However you can continue to store controls in array if the array is declared in the object type

To process several controls (for example 20 buttons)

Private Sub MyButtonHandler_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click,
Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click,
Button9.Click, Button10.Click, Button11.Click, Button12.Click,
Button13.Click, Button14.Click, Button15.Click, Button16.Click,
Button17.Click, Button18.Click, Button19.Click, Button20.Click

also you can do

dim MyTextboxes as textbox() = {txt1, txt2, txt3}

Now they are in an array. Of course, the code has to be executed after the
call to InitializeComponent. Control arrays like in VB6 are not required

Alternative syntax:

dim MyTextboxes as textbox()
MyTextboxes = New textbox(){txt1, txt2, txt3}

Here is an example how to create a control array and handler to it

Private ButtonArray(9) As Button

Public Sub AddButtons()
    For x As Integer = 0 To 9
        ButtonArray(x) = New Button
        With ButtonArray(x)
            .Tag = x
            .Text = "Button " & x
            .Size = New Size(60,20)
            .Location = New Point(20, (x * 20) + 4)
            .Parent = Me
            .Visible = True
            AddHandler .Click, AddressOf ButtonHandler
        End With
End Sub

'Then, create a ButtonHandler to handle the button clicks

Public Sub ButtonHandler(ByVal sender As System.Object, ByVal e As
    MsgBox("Button number " & CType(sender,Button).Tag & " was clicked")
End Sub
control arrays aren't supported in vb.net

but you can get to them like

for each x as control in me.controls
              If x.GetType Is GetType(TextBox) Then
                CType(x, TextBox).Text = "test"
            End If

And if you want to use the same event for several controls

you can do something like this

    Private Sub txt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textbox1.TextChanged, textbox2.TextChanged
             ctype(sender,textbox).text += "ok"
    End Sub

or you can create your own array

dim mytextboxes(10) as textbox

mytextboxes(0) = textbox1
mytextboxes(1) = textbox2


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now