Referencing Dynamically Added Controls

Posted on 2005-04-15
Medium Priority
Last Modified: 2010-04-23
Hello experts!  I have just completed code to dynamically add labels to my form.  The problem I'm having, and maybe because it's Friday at 4 PM, is if I try to add code for these controls I get an error because the controls do not yet exist.  

For example I want to change the text of the label based on a user action.  Well how I what the label is called.

Question by:QualityData
  • 2
  • 2
LVL 12

Expert Comment

ID: 13794366
it may help if you post up the code where you're adding the controls to the form... so the experts know what to work with

Author Comment

ID: 13794433
Here's the sub that creates the lablel.  Note the label name will be "lbl" & Str(lblIndex).  If I try to reference the newly created label with the name "lbl" & str(lblindex), the program gives an error because that object does not yet exist.  Hope this helps a little.

    Public Sub createLabel(ByVal Top As Single, ByVal Left As Single, ByVal lblIndex As Integer)
        Dim Lbl As Label

        Lbl = New Label
        Lbl.Name = "Lbl" & Str(lblIndex)
        Lbl.Text = lineLength
        Lbl.Font = New System.Drawing.Font("Arial", 12.0!, FontStyle.Bold)
        Lbl.Top = Top
        Lbl.Left = Left
        Lbl.Visible = True
    End Sub
LVL 12

Accepted Solution

S-Twilley earned 500 total points
ID: 13794487
maybe something like this...

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim thisControl As Control
        thisControl = GetControlByName("Button2")
        If Not thisControl Is Nothing Then
            thisControl.Text = "Button2 Mark 3"
        End If
    End Sub


    Function GetControlByName(ByVal ControlName As String) As Control
        Dim someControl As Control = Nothing

        For Each someControl In shapesPanel.Controls
            If someControl.Name.ToLower = ControlName.ToLower Then
                Return someControl
            End If

        Return Nothing
    End Function

Author Comment

ID: 13794549
I'll try it on Monday but that looks good.  Thanks, Jim

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

807 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