Array of controls

In VB 6 I were able to address an array of controls for example like
lblTest(5).caption = 10


for i = 1 to 4
   lblTest(i).caption = i*2

Is there any way to do anything similar in
Who is Participating?
Mike TomlinsonMiddle School Assistant TeacherCommented:
(I think this may be covered in one of the links posted by the others)

You can address the controls "by name" using various methods such as reflection, passing a name to a containers Controls() collection, a recursive search, or the newer Controls.Find() method.

Here is a simple example of iterating over Label1 thru Label5 using a helper function and also an example of how to "wire up" all of those Labels to the same method so they will fire in one place.  This is done with either AddHandler() or thu the use of the "Handles" keyword:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim lbl As Label
        Dim ctl As Control
        Dim ctlName As String
        For i As Integer = 1 To 5
            ctlName = "Label" & i
            ctl = GetControlByName(ctlName)
            If Not IsNothing(ctl) AndAlso TypeOf ctl Is Label Then
                lbl = CType(ctl, Label)
                lbl.Text = "I'm Label #" & i
            End If
    End Sub

    Private Function GetControlByName(ByVal ctlName As String) As Control
        Dim matches() As Control = Me.Controls.Find(ctlName, True)
        If matches.Length > 0 Then
            Return matches(0)
        End If
        Return Nothing
    End Function

    Private Sub lbl_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click, Label2.Click, Label3.Click, Label4.Click, Label5.Click
        Dim lbl As Label = CType(sender, Label)
        MessageBox.Show("Hello from " & lbl.Name)
    End Sub

End Class

Open in new window

Rakesh JaiminiCommented:
koossaAuthor Commented:
Is there any way of doing this at design time?

The main thing that I want to accomplish is to refer to a control by it's index.

txtTest(3).text = "test3"
txtTest(0).text = "test0"
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Rakesh JaiminiCommented:
have u checked above link
please go through above link properly
it contains all the ans to your queries
koossaAuthor Commented:
Yes, I have, but they said you cannot do it at design time and I need to do it at desig time.
Read the link at the bottom of the page.
The Control Array is specific to VB6 and does not supported in VB.NET. But you can simulate the control array concept using some work around solutions. But that will work at run time only .
Please refer the following links.
Creating Control Arrays in Visual Basic .NET and Visual C# .NET
Control Arrays for Visual Basic 6.0 Users
Simulating Control Arrays in VB.NET

After further googling, I found the following link.

This code example in the above shows how to make a "control array" component in VB .NET that you can use to add and remove controls from the array at design time.  Try it.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.