# use two variables to identify a form control

I am not sure if this is easily possible but I am looking for options.
I am trying to identify the name of a label by using a variable.

for example:

I have nine labels on my form : label1, label2 ...

I have a loop sorta like this:

dim x = 1
dim lbl

do while not (x=9)
lbl = label  (x)      --------> here I want the variable lbl to be set to label1 when x = 1... label2 when x =2... and so on.
NewFunction(lbl)
x = x+1
loop

any suggestions are appreciated.
thank you,
K
LVL 11
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

High School Computer Science, Computer Applications, and Mathematics TeachersCommented:
The approach really depends on what version VB.Net you have...

You running 2003 or 2005?
Commented:
Is this too simple?
Dim lbl as string
do while not (x=9)
lbl = label  (x).Name
on.
NewFunction(lbl)
x = x+1
loop

or maybe you mean
Dim lbl as Label
do while not (x=9)
lbl = label  (x)
on.
NewFunction(lbl)
x = x+1
loop
Systems and Integration DeveloperCommented:
Try:

lbl = Me.Controls("Label" + x.ToString())
High School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Using Reflection:

Dim c As Control
Dim lbl As Label
Dim i
For i = 1 to 9
c = FindControl("Label" & i)
If Not IsNothing(c) Then
If TypeOf c Is Label Then
lbl = CType(c, Lable)
NewFunction(lbl)
End If
End If
Next i

Protected Overridable Function FindControl(ByVal Name As String) As Control
Dim propInfo As System.Reflection.PropertyInfo
propInfo = Me.GetType().GetProperty(Name, System.Reflection.BindingFlags.IgnoreCase Or System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Public)
If Not propInfo Is Nothing Then
Dim value As Object = propInfo.GetValue(Me, Nothing)
If TypeOf value Is Control Then
Return CType(value, Control)
End If
End If
Return Nothing
End Function

Or using Recursion:

Dim c As Control
Dim i
For i = 1 to 9
c = GetControlByName("Label" & i, Me)
If Not IsNothing(c) Then
If TypeOf c Is Label Then
lbl = CType(c, Lable)
NewFunction(lbl)
End If
End If
Next i

Private Function GetControlByName(ByVal ctlName As String, ByVal container As Control) As Control
Dim c As Control
For Each c In container.Controls
If c.Name.ToLower.Equals(ctlName.ToLower) Then
Return c
ElseIf c.Controls.Count > 0 Then '                  <---- If it's a container...
Return GetControlByName(ctlName, c) '       <--- make the recursive call
End If
Next
End Function

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
doh.  In another question I asked I mentioned that I am using 2005 express.  I forgot that here.

I tried both those and both seemed to confuse vb.net

Carl,
I tried that but I get a null value error at debug when trying to use

lbl.text = "cat"

thanks all... any other ideas?
High School Computer Science, Computer Applications, and Mathematics TeachersCommented:
carl_tawns suggestion will work in VB.Net 2005 but ONLY if the Labels are directly on the form and not inside another container such as a Panel or GroupBox.

For Labels contained anywhere on a Form in VB.Net 2005 you can do something like:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim c As Control
Dim lbl As Label
Dim i As Integer
For i = 1 To 9
c = GetControlByName("Label" & i, Me)
If Not IsNothing(c) Then
If TypeOf c Is Label Then
lbl = CType(c, Label)
lbl.Text = "Hello Label" & i
End If
Else
End If
Next
End Sub

Private Function GetControlByName(ByVal ctlName As String, ByVal frm As Form) As Control
Dim ctls() As Control = frm.Controls.Find(ctlName, True)
If ctls.Length > 0 Then
Return ctls(0)
Else
Return Nothing
End If
End Function

Author Commented:

Idle_mind,

ahhhh. I see. ... I think
My label is located on a tab page.  Would that cause carl_tawn's suggestion not to work?

I used your recursion method and it seems to work.  I will have to tweak a bit but looks good so far.

thanks all,
K
High School Computer Science, Computer Applications, and Mathematics TeachersCommented:
"My label is located on a tab page.  Would that cause carl_tawn's suggestion not to work?"

That's correct.

Since you have VB.Net 2005 Express, be sure to look at the GetControlByName() function in my very last post.  The recursion approach is necessary in VB.Net 2003 and below...
Author Commented:
excellent... thanks everyone
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.