Array of controls VB.net

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

or

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

Is there any way to do anything similar in vb.net?
koossaAsked:
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
        Next
    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

0
 
Rakesh JaiminiCommented:
0
 
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"
0
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
0
 
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.
0
 
RameshSCommented:
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
 
 
 
0
 
RameshSCommented:

After further googling, I found the following link.

http://www.vb-helper.com/howto_net_control_arrray.html

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.
0
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.