Reputation: 1269
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 ?
Upvotes: 1
Views: 974
Reputation: 16187
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)
Upvotes: 1