Search code examples
c#winformsradio-buttongroupbox

Get if any RadioButton 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 application is:

A file gets selected → The GroupBox gets enabled → A Panel/ComboBox, TextBox gets enabled depending on the selected RadioButton


Solution

  • 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 
    }