?
Solved

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

Posted on 2003-03-16
6
Medium Priority
?
553 Views
Last Modified: 2010-02-07
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 :-)
0
Comment
Question by:eDogg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 8149961
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.
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 8151632
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
    Next        
End Sub

'Then, create a ButtonHandler to handle the button clicks

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

0
 

Author Comment

by:eDogg
ID: 8151827
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?
0
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 
LVL 28

Expert Comment

by:iboutchkine
ID: 8151922
You can iterate throug controls like that

Dim c As Control
        For Each c In Me.Controls
            Debug.WriteLine(c.GetType())
        Next

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

 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
            MsgBox(c.Name)
        Next
    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
            'DO SOMETHING WITH THIS CONTROL (FOR EXAMPLE RESIZE)
           If ctl.Controls.Count > 0 Then
                ModifyInternalSize(ctl)
           End If
        Next
    End Sub
0
 

Author Comment

by:eDogg
ID: 8152210
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?

0
 
LVL 28

Accepted Solution

by:
iboutchkine earned 200 total points
ID: 8152287
I have never noticed any performance problems with that.
0

Featured Post

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

765 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