Enable all LinkLabel controls

你离开我真会死。 提交于 2019-12-11 14:33:35

问题


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?


回答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)


来源:https://stackoverflow.com/questions/30776937/enable-all-linklabel-controls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!