Hi,
How can I dynamically find all of the linklabel within a form, then get the linklabel name?

Dani AI

Generated

— good thread. was right to ask whether your LinkLabels live directly on the Form or inside containers; that changes enumeration. 's stack-based traversal is a valid technique for nested controls. Below is a compact, practical alternative (recursive) plus a few pitfalls to watch for so the name-to-database check is robust and fast.

Preferred pattern: fetch all matching names from the database once (store them in a HashSet with a case-insensitive comparer), then walk the control tree and compare each LinkLabel's Name (see Control.Name) or a stable identifier you place in Tag. Avoid hitting the DB per control — that is slow and fragile. Use a recursive routine so nested containers (Panel, GroupBox, TableLayoutPanel, etc.) are handled automatically; note that ToolStripItems are not in Control.Controls and need separate handling if used (Control.Controls, LinkLabel).

Example pattern (VB.NET):

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim dbNames As New HashSet(Of String)(StringComparer.OrdinalIgnoreCase)
    ' Populate dbNames from DB here (one query)
    ApplyHighlights(Me, dbNames)
End Sub

Private Sub ApplyHighlights(parent As Control, dbNames As HashSet(Of String))
    For Each c As Control In parent.Controls
        If TypeOf c Is LinkLabel Then
            Dim ll = DirectCast(c, LinkLabel)
            If dbNames.Contains(ll.Name) Then ll.BackColor = Color.LightGreen
        End If
        If c.HasChildren Then ApplyHighlights(c, dbNames)
    Next
End Sub

Troubleshooting notes: if LinkLabels are created after Load, call the routine after creation or handle ControlAdded. If you fetch DB values on a background thread, marshal UI updates with Invoke. Consider using Tag for a stable key if designer-generated Name values change. If results still don’t appear, add a quick debug dump of control names to confirm your enumeration reaches every container.

Recommended Answers

All 4 Replies

thanks for this one dude,.but when i tried that and it didn't work..it seems that it didn't fit to what i want to happen.

here's the scenario..'

http://www.daniweb.com/software-development/vbnet/threads/378064

you'd already answered there my question about searching the label's name within the form.

now, what i want is, when my form loads,.it will detect each label's name..for example(ex. of my label's name lbl23WS001).,then it will compare the label's name to my database if it exist..if it exist in the database, it will then change it's backcolor. :)

Are all the LinkLabels on the Form or are they in separate containers as Panels?
If in separate containers, then you need to loop thruough each container's controls, not thruough the Form's controls. A simple Sub with a Parameter should do this just nicely.

If you still have issues with this, post the code that you have so far.

Member Avatar for Member #857553
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim StackOfControls As New Stack(Of Control)
        Dim AllLinkLabels As New List(Of LinkLabel)
        Dim ControlChecking As Control

        StackOfControls.Push(Me)
        Do While StackOfControls.Count > 0
            ControlChecking = StackOfControls.Pop
            For Each ChildControl As Control In ControlChecking.Controls
                If TypeOf ChildControl Is LinkLabel Then
                    AllLinkLabels.Add(ChildControl)
                End If
                If ChildControl.Controls.Count > 0 Then
                    StackOfControls.Push(ChildControl)
                End If
            Next
        Loop

        'All link labels inside AllLinkLabels
        'Iterate through the list and get the name
    End Sub
commented: impressive :) +11
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.