Click here to Skip to main content
15,867,453 members
Articles / Web Development / ASP.NET
Alternative
Tip/Trick

Disable Child Controls Recursively

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
13 Nov 2010CPOL 6.2K   2  
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Egypt Egypt
Oil & Gas Engineer
C# & VB.net
Coding For Fun Only

Comments and Discussions

 
-- There are no messages in this forum --