Click here to Skip to main content
11,701,855 members (69,462 online)
Click here to Skip to main content

Disable Child Controls Recursively

, 13 Nov 2010 CPOL 2.1K 2
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

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

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 13 Nov 2010
Article Copyright 2010 by a_pess
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid