I have lot of dynamically generated radio buttons in my Windows Forms project. They may be checked based on the values in a database. I want to clear all radio buttons in a
Both will do it below, as long as _radioContainer is a GroupBox.
private void button1_Click(object sender, EventArgs e) {
// This will remove the radiobuttons completely...
_radioContainer.Controls.OfType().ToList().ForEach(p => _radioContainer.Controls.Remove(p));
// Either of the below will clear the checked state
_radioContainer.Controls.OfType().ToList().ForEach(p => p.Checked = false);
foreach (RadioButton radio in _radioContainer.Controls.OfType().ToList()) {
if (radio.Checked == true) {
radio.Checked = false;
break;
}
}
}