65.9K
CodeProject is changing. Read more.
Home

Disable Child Controls Recursively

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Nov 2, 2010

CPOL
viewsIcon

6392

Alternative For VB.NET Windows Forms Private Sub ControlEnabled(ByVal ctrl As Control, ByVal isDisable As Boolean) Me.ControlEnabled(ctrl, isDisable, True) End Sub Private Sub ControlEnabled(ByVal ctrl As Control, ByVal isDisable As Boolean, ByVal allowRecurse As...

Alternative For VB.NET Windows Forms
    Private Sub ControlEnabled(ByVal ctrl As Control, ByVal isDisable As Boolean)
        Me.ControlEnabled(ctrl, isDisable, True)
    End Sub

    Private Sub ControlEnabled(ByVal ctrl As Control, ByVal isDisable As Boolean, ByVal allowRecurse As Boolean)
        If (allowRecurse AndAlso ctrl.HasChildren) Then
            For Each c As Control In ctrl.Controls
                ControlEnabled(c, isDisable, True)
            Next
        End If

        If Not TypeOf ctrl Is Form Then
            If (Not ctrl.Enabled = isDisable) Then
                ctrl.Enabled = isDisable
            Else
                Try
                    ctrl.Enabled = isDisable
                Catch ex As Exception
                    ctrl.Enabled = ctrl.Enabled
                End Try
            End If
        End If
    End Sub
Aslo, the same may be utilized with different properties as Color, Font, ..... whatever else.
''''''''''''
    Private Sub ControlBackColors(ByVal ctrl As Control, ByVal clr As Color)
        Me.ControlBackColors(ctrl, clr, True)
    End Sub

    Private Sub ControlBackColors(ByVal ctrl As Control, ByVal clr As Color, ByVal allowRecurse As Boolean)
        If (allowRecurse AndAlso ctrl.HasChildren) Then
            For Each c As Control In ctrl.Controls
                ControlBackColors(c, clr, True)
            Next
        End If
        If Not TypeOf ctrl Is Form Then
            If (Not ctrl.BackColor = clr) Then
                If clr <> Color.Transparent Then
                    ctrl.BackColor = clr
                Else
                    Try
                        ctrl.BackColor = clr
                    Catch ex As Exception
                        ctrl.BackColor = SystemColors.Control
                    End Try
                End If
            End If
        End If
    End Sub

    Private Sub ControlFonts(ByVal ctrl As Control, ByVal fnt As Font)
        ControlFonts(ctrl, fnt, True)
    End Sub

    Private Sub ControlFonts(ByVal ctrl As Control, ByVal fnt As Font, ByVal allowRecurse As Boolean)
        If (allowRecurse AndAlso ctrl.HasChildren) Then
            For Each c As Control In ctrl.Controls
                ControlFonts(c, fnt, True)
            Next
        End If

        If Not TypeOf ctrl Is Form Then
            If (Not ctrl.Font Is fnt) Then
                If (fnt.Style = ctrl.Font.Style) Then
                    ctrl.Font = fnt
                Else
                    Try
                        ctrl.Font = New Font(fnt, ctrl.Font.Style)
                    Catch ex As Exception
                        ctrl.Font = fnt
                    End Try
                End If
            End If
        End If
    End Sub
Usage:
ControlBackColors(Me, Color.Red)
ControlEnabled(Me, False)
'
' and so on
I am not sure if this way will work for ASP.NET or not.... someone has to check.