I am trying to uncheck all the checkboxes in all the panels I have. This is different from other questions because it is for every panel that has a checkbox. The code I have right now unchecks all the checkboxes in the form but not in all the panels.
foreach (CheckBox control in this.Controls.OfType<CheckBox>())
{
control.Checked = false;
}
I have a bunch of panels and checkboxes in them and there has to be a simple way to uncheck them all.
this does it to every checkbox in my form instead of only panels my code:
private void button16_Click(object sender, EventArgs e)
{
clearStuff((Control.ControlCollection)this.Controls);
}
private void clearStuff(Control.ControlCollection obj)
{
foreach (Control control in obj)
{
if (control.HasChildren)
clearStuff(control.Controls);
else
if (control is CheckBox)
{
CheckBox check_box = (CheckBox)control;
check_box.Checked = false;
}
}
}
this works but it could be refactored
foreach (Panel control2 in this.Controls.OfType<Panel>())
{
foreach (CheckBox control1 in control2.Controls.OfType<CheckBox>())
{
control1.Checked = false;
}
}