简体   繁体   中英

Unchecked all checkbox in a form inside a groupbox inside a panel and another panel

I'm getting tired of clearing all checkboxes inside a groupbox inside another panel in my form. I have a code that can clear all checkboxes in my form inside the panel221.

For Each element As Control In Me.Panel221.Controls
        If TypeOf element Is CheckBox Then
            DirectCast(element, CheckBox).Checked = False
        End If
Next

I dont know why I can't clear all checkboxes in my form by just using this code.

For Each element As Control In Me.Controls
            If TypeOf element Is CheckBox Then
                DirectCast(element, CheckBox).Checked = False
            End If
Next

This is the structured of my form:

-FORM
 -PANEL 1
  -GroupBox 1
   -PANEL 2
     -PANEL 3
       * Several CheckBox inside another panel
     -END OF PANEL 3
   -END OF PANEL 2
  -END OF GROUPBOX 1
   'Another groupbox
  -GroupBox 2
   -PANEL 4
     -PANEL 5
       * Several CheckBox inside another panel
     -END OF PANEL 5
   -END OF PANEL 4
  -END OF GROUPBOX 2
 -END OF PANEL 1
-END OF FORM

How can I clear or unchecked all that checkbox under a several groupbox and panel ?

Me.Controls contains only direct children, no children of its children and so on. In order to do what you want, you have to use a recursive function :

Private Sub UncheckRecursive(ByVal element As Control)
    If TypeOf element Is CheckBox Then
        DirectCast(element, CheckBox).Checked = False
    Else
        For Each childElement In element.Controls
            Me.UncheckRecursive(childElement)
        Next
    End If
End Sub

Then you call it this way : me.UncheckRecursive(me)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM