Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Array of controls VB.net

Posted on 2009-12-26
Medium Priority
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 vb.net?
Question by:koossa
  • 2
  • 2
  • 2
  • +1
LVL 10

Expert Comment

by:Rakesh Jaimini
ID: 26124283

Author Comment

ID: 26124292
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
ID: 26124297
have u checked above link
please go through above link properly
it contains all the ans to your queries
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.


Author Comment

ID: 26124303
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

ID: 26124373
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

ID: 26124388

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 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 26124418
(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


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

580 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