Array of controls

Posted on 2009-12-26
Last Modified: 2012-05-08
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
Question by:koossa
    LVL 10

    Expert Comment

    by:Rakesh Jaimini

    Author Comment

    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"
    LVL 10

    Expert Comment

    by:Rakesh Jaimini
    have u checked above link
    please go through above link properly
    it contains all the ans to your queries

    Author Comment

    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.
    LVL 10

    Expert Comment

    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
    LVL 10

    Expert Comment


    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.
    LVL 85

    Accepted Solution

    (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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now