I have a form that is dynamically created, on this form are several radio buttons that are created at runtime. There is a button on this form eg “Next” when the user clicks on the next I want to loop through and check if one of the radio buttons are checked before I continue, I have tried the following:
void nextButton_Click(object sender, EventArgs e)
{
foreach (Control c in _form.Controls)
{
if (c is RadioButton)
{
RadioButton radio = c as RadioButton;
if (radio is RadioButton)
{
if (radio.Checked == true)
{
//code continue to next
}
else
{
MessageBox.Show("You must select at least one.");
}
}
}
}
}
Kind regards
geo
You can use Linq to make it simpler
–EDIT–
I updated the answer to recursively search all controls.