0

我正在尝试创建一个通用过程,只需使用您可以看到的参数调用此过程,即可将“X”控件对象的可见属性更改为“False/True”:

    ' Desired usage:
    ' Disable_Controls(CheckBox, Me.Panel1.Controls, False)

    Public Sub Disable_Controls(ByVal ControlType As Control, _
                                ByVal Container As ControlCollection, _
                                ByVal Visible As Boolean)

        For Each Control As Control In Container
            ' If TypeOf Control Is CheckBox then...
            If TypeOf Control Is Control Then
                Control.Visible = Visible
            End If
        Next

    End Sub

问题是我无法像尝试那样传递控件名称(“Checkbox”),我已经尝试使用“CType(Control,CheckBox)”但没有奏效。

我怎么能这样做?

4

1 回答 1

1
Public Sub Disable_Controls(Of T As Control)(ByVal Container As Control, _
                            ByVal Visible As Boolean)
    For Each ctrl As T In Container.Controls.OfType(Of T)()
        ctrl.Visible = Visible
    Next
End Sub

像这样称呼它:

Disable_Controls(Of Checkbox)(MyGroupbox, False)
于 2013-06-04T20:36:38.367 回答