Assuming that I wanted to check or un-check all the CheckBoxes in a container Control, like a Panel:
private void SetAllCheckBoxesCheckState(Control container, bool isChecked)
{
foreach (CheckBox cbx in container.Controls.OfType<CheckBox>())
{
cbx.Checked = isChecked;
}
}
Note that using Controls.OfType<SomeControlType> only returns the top-level Controls in its target container whose Type matches (it is not recursive): if you have a CheckBox control in a panel within a Panel, .OfType will not find it.