How do I get which radio button is checked from a groupbox?

Amit Bisht picture Amit Bisht · Aug 31, 2013 · Viewed 78.8k times · Source

I have these groupboxes:

Enter image description here

I want to run some code according to checked true state of a radio button like:

string chk = radiobutton.nme; // Name of radio button whose checked is true
switch(chk)
{
    case "Option1":
        // Some code
        break;

    case "Option2":
        // Some code
        break;

    case "Option3":
        // Some code
        break;
}

Is there any direct way so that I can only get the name of the checked radio button?

Answer

Soner Gönül picture Soner Gönül · Aug 31, 2013

You can find all checked RadioButtons like

var buttons = this.Controls.OfType<RadioButton>()
                           .FirstOrDefault(n => n.Checked);

Also take a look at CheckedChanged event.

Occurs when the value of the Checked property changes.