Question How to create a ControlArray instead of a non-working For-Function!?


New member
Aug 7, 2011
Liesing, Wien, Austria, Austria
Programming Experience
Hi everyone,

first of all - I hope i posted in the right Forum. My Problem is, that I have some Buttons, Labels and TextBoxes I need to generate after the User told a number of Players. With the Code I'm using now only the last Player will have these Controls working, because they are moved - for example - from Player 1 to Player 3.

I was looking for several hours in the Internet with Google and so on and found out, that a "ControlArray" should help out, BUT I did not find any help on how to create such a ControlArray. The biggest Problem I have is, that these buttons have to have names like "1", "2", "3" and so on (or if it works somehow to read out the Index of a ControlArray the names will not be necessary).

This is the code I am using right now to generate the Controls, but as you see it won't work, because the properties will be changed when the For-Function restarts.What I need is a hint on how to get these Controls/Buttons into a Control-Array and then read out the Index-Number to use them in the Functions that are called. Hopefully you know, what I mean.

Thanks everybody for reading and help! :encouragement:

EDIT: Got it allready! It's very easy, if you know how to do it :D :
Public Class Form1
Private btnTestNumButtons() As System.Windows.Forms.Button

    Private Sub CreateButtonArray()
        Dim shtIndex As Short

        ReDim btnTestNumButtons(10)
        For shtIndex = 0 To 9
            btnTestNumButtons(shtIndex) = New System.Windows.Forms.Button
            With btnTestNumButtons(shtIndex)
                .Text() = shtIndex.ToString()    'Set text caption
                .Size() = New Size(40, 40)     'Set the button size
                .FlatStyle() = FlatStyle.Popup  'Set the button flat style
                .BackColor() = System.Drawing.Color.Blue       'Set the background color
                AddHandler .Click, AddressOf Me.btnTestNumButtons_Click    'Attach handler reference
            End With


    End Sub

This worked with some changes for me!
Last edited:


VB.NET Forum Moderator
Staff member
Aug 17, 2004
Sydney, Australia
Programming Experience
There is no such thing as a ControlArray. That was a VB6 thing. In VB.NET, arrays of controls are exactly the same as any other array. If you want to create an array then just go ahead and create an array. Create the Buttons or whatever other controls you need and assign them to the elements of the array. That's all there is to it. Just like any other array. E.g.
Dim buttons(2) As Button

buttons(0) = New Button
buttons(1) = New Button
buttons(2) = New Button
Top Bottom