24

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amit Bisht
  • 4,870
  • 14
  • 54
  • 83
  • 1
    possible duplicate of [Which Radio button in the group is checked?](http://stackoverflow.com/questions/1797907/which-radio-button-in-the-group-is-checked) – Tot Zam Aug 14 '15 at 14:20

5 Answers5

40

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.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • @Soner I tried to use this code, but it provides a error of ambiguity between ´Forms.RadioButton´ and ´Controls.RadioButton´ , what does it means? – César Amorim Aug 24 '14 at 21:24
  • @CésarAmorim Both `System.Windows.Controls` and `System.Windows.Forms` have `RadioButton` class. I feel like, in your project both these namepsaces are added and compiler confuses which namespace belongs on this `RadioButton` when you type it only it's name. As a solution, you can use it's full name to prevent this situation like `System.Windows.Controls.RadioButton` or `System.Windows.Forms.RadioButton`. – Soner Gönül Aug 25 '14 at 06:18
  • 3
    Had some trouble following this for Universal Windows Apps, as `Windows.UI.Xaml.Controls.Page` doesn't have `Controls` property there, so it's best to use some encapsulating WPF element, like named `StackPanel` or `Grid` and `Children` property: `rbPanel.Children.OfType().FirstOrDefault(r => r.IsCheked.Value);` (`IsChecked` is nullable, hence the `.Value`) – Ignas Vyšnia Oct 02 '15 at 10:29
  • Would someone be able to explain the .FirstOrDefault(n => n.Checked); line for me please? – Paul Alexander Mar 10 '17 at 09:15
  • @PaulAlexander `OfType` method returns `IEnumerable` which means an iterable collections of `RadioButton`'s. `FirstOrDefault` method returns the first element (or `default` if can't find any element satisfied _that_ condition) which satisfied with given condition, which is `n.Checked` (that means checked) in our case. Based on radio button structure, it can be _only_ one selected item, so this code does exactly what OP wants. For more information, check `Enumerable` class from MSDN: https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable – Soner Gönül Sep 10 '20 at 11:55
10
groupbox1.Controls.OfType<RadioButton>().FirstOrDefault(r => r.Checked).Name

this will get the name of checked radio button. If you want to use it later, you might store name of that by storing into variable.

Cheers

Meet Vasani
  • 156
  • 1
  • 6
4

You should take some look at the CheckedChanged event to register the corresponding event handler and store the Checked radio button state in some variable. However, I would like to use LINQ here just because you have just some RadioButtons which makes the cost of looping acceptable:

var checkedRadio = new []{groupBox1, groupBox2}
                   .SelectMany(g=>g.Controls.OfType<RadioButton>()
                                            .Where(r=>r.Checked))
// Print name
foreach(var c in checkedRadio)
   System.Diagnostics.Debug.Print(c.Name);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
King King
  • 61,710
  • 16
  • 105
  • 130
0

Rather than checking all RadioButtons, use the Validated event of the GroupBox.

private void grpBox_Validated(object sender, EventArgs e)
    {
        GroupBox g = sender as GroupBox;
        var a = from RadioButton r in g.Controls where r.Checked == true select r.Name;
        strchecked = a.First();
     }
-1

In my opinion, it's better if you use RadioGroup instead of GroupBox. If you use radioGroup, you can always find the selected item easily like this:

radioGroup.selectedIndex;

If you design using Windows Forms, I suggest to implement RadioGroup behavior like this (please note that my code is in Java):

for (Component comp:groupBox1.components) {
    if (((RadioButton)comp).selected)
        return ((RadioButton)comp).value;
}

You can put this code block in a method to return selected radioButton value, and then you can use this value in your SWITCH part.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Masoud Sahabi
  • 223
  • 1
  • 3
  • 9