Which Radio button in the group is checked?
Solution 1:
You could use LINQ:
var checkedButton = container.Controls.OfType<RadioButton>()
.FirstOrDefault(r => r.Checked);
Note that this requires that all of the radio buttons be directly in the same container (eg, Panel or Form), and that there is only one group in the container. If that is not the case, you could make List<RadioButton>
s in your constructor for each group, then write list.FirstOrDefault(r => r.Checked)
.
Solution 2:
You can wire the CheckedEvents of all the buttons against one handler. There you can easily get the correct Checkbox.
// Wire all events into this.
private void AllCheckBoxes_CheckedChanged(Object sender, EventArgs e) {
// Check of the raiser of the event is a checked Checkbox.
// Of course we also need to to cast it first.
if (((RadioButton)sender).Checked) {
// This is the correct control.
RadioButton rb = (RadioButton)sender;
}
}
Solution 3:
For those without LINQ:
RadioButton GetCheckedRadio(Control container)
{
foreach (var control in container.Controls)
{
RadioButton radio = control as RadioButton;
if (radio != null && radio.Checked)
{
return radio;
}
}
return null;
}