• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 566
  • Last Modified:

Use a variable to refer to a control name in VB.NET?

I'm new to VB.NET and need to ask a fairly basic question:

In VB6, I could use a variable or combination of literals and variables to refer to a control on a form. For example, if I had a variable called intNumber and controls named "lbl10", "lbl11", "lbl12", etc, I could use the following code to refer to a control, and gain access to all of its methods and properties:

Me("lbl" & intNumber).Text = "new text"

I cannot find how to do this in VB.NET. When I attempt to use the above syntax, it gives me some error about being unable to index an object without a default property, or about not being able to implicitly convert a string to an integer.

Can you still use my variable + text method to refer to a control? or do I have to know the control's index number in order to refer to it? Is there a better method altogether to refer to a control without being able to "hard code" its control name?

Please help! Thank you :-)
  • 3
  • 2
1 Solution
You can iterate through Form.Controls collection and check the Name property for each control. When the controll with desired name is found, set it's Text.
VB.NET  no longer supports control arrays(control collection that share the same name).
However you can continue to store controls in array if the array is declared in the object type

Private ButtonArray(9) As Button

Public Sub AddButtons()
    For x As Integer = 0 To 9
        ButtonArray(x) = New Button
        With ButtonArray(x)
            .Tag = x
            .Text = "Button " & x
            .Size = New Size(60,20)
            .Location = New Point(20, (x * 20) + 4)
            .Parent = Me
            .Visible = True
            AddHandler .Click, AddressOf ButtonHandler
        End With
End Sub

'Then, create a ButtonHandler to handle the button clicks

Public Sub ButtonHandler(ByVal sender As System.Object, ByVal e As
    MsgBox("Button number " & CType(sender,Button).Tag & " was clicked")
End Sub

eDoggAuthor Commented:
Iterating through the Form.Controls collection sounds like a good idea. Could you provide the code to do it?

Here's what I tried and it's not working:

            Dim arrAI() As Integer = CGame.AIMove()
            Dim name As String
            Dim ctrl As Windows.Forms.Control

            name = "lbl" & arrAI(0).ToString & arrAI(1).ToString
            For Each ctrl In Me.Controls
                If ctrl.Name = name Then
                    Exit For
                End If
            Next ctrl

However, when this code executes I get "invalid type casting" or some such and it breaks on the "For Each" line.

Also, a control array would not necessarily be a good solution to my problem, because sometimes it won't be the same type of control, but maybe an entirely different type.

Is there no way to just build a control name from a string and then convert that to a control type?

Is there a performance hit for iterating through every control on a form instead of just referring to it directly?
Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

You can iterate throug controls like that

Dim c As Control
        For Each c In Me.Controls

but this code will not give you controls within containers. If you want controls within container

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim c As Control
        For Each c In Me.GroupBox1.Controls
    End Sub

If you want all the controls including ones within container you can loop recursivly

Private Sub ModifyInternalSize(ByVal ParentControl As Control)
        Dim ctl As Object
        For Each ctl In ParentControl.Controls
           If ctl.Controls.Count > 0 Then
           End If
    End Sub
eDoggAuthor Commented:
This solution works (THANK YOU) and I will accept it as answer shortly, but is there any performance hit for doing it this way? What if there are 100+ controls on a form? Or what if I need to search across numerous forms?

Is it correct to say that you can no longer use a string to build a control name, like you could in VB6?

I have never noticed any performance problems with that.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now