Thorvason Thorvason - 3 months ago 28
C# Question

Get if any radio button in a Groupbox was checked

I have a Windows Forms application, where I have several radio buttons stored in a

GroupBox
. I need to enable a different
GroupBox
based on the selected radio button.

groubBox.Enter
doesn't seem to be the
EventHandler
I was looking for. Is there any way to do it my way or do I have to create a handler for each
radiobutton.CheckedChanged
?

Edit

The workflow of the applikation is: a file gets selected -> the groupbox gets enabled -> a panel/ combobox, textbox gets enabled depending on the selected radiobutton

Edit Solution

Sometimes stackoverflow recommends way better than google searches...

Event handler for groupBox with radioButtons in C#

This with un-luckys answer is the solution

Answer

Let gbRadioButtons be the name of the GroupBox, Then you can iterate though each radioButton in that particular Groupbox and check whether it is selected or not by using the following code(include this code where you want to check):

bool isAnyRadioButtonChecked = false;
foreach (RadioButton rdo in gbRadioButtons.Controls.OfType<RadioButton>())
{
    if (rdo.Checked)
    {
        isAnyRadioButtonChecked=true;
        break;
    }
}
if (isAnyRadioButtonChecked)
{ 
  // Code here one button is checked
}
else
{
  // Print message no button is selected 
}
Comments