Referencing Dynamically Added Controls

Posted on 2005-04-15
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
    LVL 12

    Expert Comment

    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

    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

    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

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    This video discusses moving either the default database or any database to a new volume.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    758 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now