Clearing checkboxes within groupboxes
I have a form wit开发者_StackOverflow中文版h several groupboxes, each containing controls several checkboxes. I want to clear the checkboxes.
I use the following code. However it does not get cleared.
What am i doing wrong?
foreach (Control ctrl in this.Controls)
{
if (ctrl is CheckBox)
((CheckBox)(ctrl)).Checked = false;
}
Once again the checkboxes are within groupboxes.
When the checkboxes are within another control, in your case groupboxes, you need to use recursion to set the property checked of the checkboxes. The collection this.Controls only returns the closest child controls.
setCheckBoxesUnChecked(this);
public function setCheckBoxesUnChecked(Control parent)
{
foreach (Control ctrl in parent.Controls)
{
if (ctrl is CheckBox)
((CheckBox)ctrl).Checked = false;
setCheckBoxesUnChecked(ctrl);
}
}
精彩评论