Question

I'm trying to make a generic procedure to change the visible property of "X" control object to "False/True" just by calling this procedure with the arguments you can see:

    ' 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

The problem is I can't pass the control name ("Checkbox") like I'm trying to do it, I've tried some things using "CType(Control, CheckBox)" but did not work.

How I can do that?

Was it helpful?

Solution

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

Call it like this:

Disable_Controls(Of Checkbox)(MyGroupbox, False)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top