?
Solved

use two variables to identify a form control

Posted on 2006-04-13
9
Medium Priority
?
1,044 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:chad
9 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16447645
The approach really depends on what version VB.Net you have...

You running 2003 or 2005?
0
 
LVL 4

Expert Comment

by:broadbent
ID: 16447677
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
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 600 total points
ID: 16447686
Try:

  lbl = Me.Controls("Label" + x.ToString())
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1400 total points
ID: 16447749
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
0
 
LVL 11

Author Comment

by:chad
ID: 16447891
doh.  In another question I asked I mentioned that I am using 2005 express.  I forgot that here.

Broadbent,
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?
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1400 total points
ID: 16447893
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
                MessageBox.Show("Label" & i, "Control Not Found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            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

0
 
LVL 11

Author Comment

by:chad
ID: 16448004

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
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16448318
"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...
0
 
LVL 11

Author Comment

by:chad
ID: 16448406
excellent... thanks everyone
0

Featured Post

Technology Partners: 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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline
Suggested Courses

839 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