0

I have a few links which are disabled by default on a form, each using a LinkLabel control.

Depending on some user interaction I need to enable either one or all of the LinkLables. I can enable the single LinkLabel just fine, but I can't find a way of enabling all of them.

In the example below I'm trying to enable all controls (as a test of my methodology), but that fails and the LinkLabels are not enabled at all.

Therefore my question is two part -

  1. How can I identify only LinkLabel controls?
  2. How can I loop throught these controls and enable them?

Here is what I have so far -

Private Sub EnableLink(Optional ByRef linkLabel As LinkLabel = Nothing)

    If linkLabel Is Nothing Then    ' Enable all links
        For Each singleLink In Me.Controls
            singleLink.Enabled = True
        Next
    Else                            ' Enable a single link
        linkLabel.Enabled = True
    End If

End Sub

Bonus question - I may need to separate my LinkLabels in to two sections, so is there a way of identifying LinkLabels which are placed within a specific control, such as a Panel or TableLayoutPanel?

David Gard
  • 11,225
  • 36
  • 115
  • 227

1 Answers1

1

You can test if a control is a LinkLabel using this code:

For Each ctrl as Control In Me.Controls
    If TypeOf ctrl Is LinkLabel Then ctrl.Enabled = True
Next ctrl

If you put your LinkLabel in a container (such as Panel or TableLayoutPanel) you can use a function like this:

Private Sub EnableAllLinkLabels(ByVal ctrlContainer As Control, ByVal blnEnable As Boolean)

    If ctrlContainer.HasChildren Then

        For Each ctrl As Control In ctrlContainer.Controls

            If TypeOf ctrl Is LinkLabel Then
                ctrl.Enabled = blnEnable
            ElseIf TypeOf ctrl Is Panel Or TypeOf ctrl Is TableLayoutPanel Then
                EnableAllLinkLabels(ctrl, blnEnable)
            End If          

        Next ctrl

    End If

End Sub

This function works also if you put a container inside another container (i.e.: a GroupBox in a Panel).

To enable all LinkLabel in a Form use this code to call the function:

EnableAllLinkLabels(Me, True)

if you want to disable only the LinkLabel in Panel3 you can use this code:

EnableAllLinkLabels(Me.Panel3, False)
tezzo
  • 10,858
  • 1
  • 25
  • 48
  • Sadly no dice, although good call on the addition of `as Control` in the `For Each` statement. – David Gard Jun 11 '15 at 09:43
  • Is your LinkLabel already in a Panel? If yes then simply change Me.Controls in YourPanel.Controls. – tezzo Jun 11 '15 at 09:46
  • Yes it is, and your suggestion works (although it's `Me.MyPanel.Controls`). Does that mean I'll always have to specify a `Panel` then? Meaning that if I do split the `LinkLabels` in to multiple `Panels` I'd first have to identify the two `Panels` and then find the `LinkLabels` within? – David Gard Jun 11 '15 at 09:51