Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Disable Child Controls Recursively

, 13 Nov 2010
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

Comments and Discussions

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