Which Radio button in the group is checked?

334,072

Solution 1

You could use LINQ:

var checkedButton = container.Controls.OfType<RadioButton>()
                                      .FirstOrDefault(r => r.Checked);

Note that this requires that all of the radio buttons be directly in the same container (eg, Panel or Form), and that there is only one group in the container. If that is not the case, you could make List<RadioButton>s in your constructor for each group, then write list.FirstOrDefault(r => r.Checked).

Solution 2

You can wire the CheckedEvents of all the buttons against one handler. There you can easily get the correct Checkbox.

// Wire all events into this.
private void AllCheckBoxes_CheckedChanged(Object sender, EventArgs e) {
    // Check of the raiser of the event is a checked Checkbox.
    // Of course we also need to to cast it first.
    if (((RadioButton)sender).Checked) {
        // This is the correct control.
        RadioButton rb = (RadioButton)sender;
    }
}

Solution 3

For those without LINQ:

RadioButton GetCheckedRadio(Control container)
{
    foreach (var control in container.Controls)
    {
        RadioButton radio = control as RadioButton;

        if (radio != null && radio.Checked)
        {
            return radio;
        }
    }

    return null;
}

Solution 4

The OP wanted to get the checked RadioButton BY GROUP. While @SLaks' answer is excellent, it doesn't really answer the OP's main question. To improve on @SLaks' answer, just take the LINQ one step further.

Here's an example from my own working code. Per normal WPF, my RadioButtons are contained in a Grid (named "myGrid") with a bunch of other types of controls. I have two different RadioButton groups in the Grid.

To get the checked RadioButton from a particular group:

List<RadioButton> radioButtons = myGrid.Children.OfType<RadioButton>().ToList();
RadioButton rbTarget = radioButtons
      .Where(r => r.GroupName == "GroupName" && r.IsChecked)
      .Single();

If your code has the possibility of no RadioButtons being checked, then use SingleOrDefault() (If I'm not using tri-state buttons, then I always set one button "IsChecked" as a default selection.)

Solution 5

You can use the CheckedChanged event for all your RadioButtons. Sender will be the unchecked and checked RadioButtons.

Share:
334,072
Billy
Author by

Billy

I am 9 foot 3 inches, 230 pounds and I enjoy fishing, hiking, long walks on the beach... Oh wait wrong website for that!

Updated on December 01, 2020

Comments

  • Billy
    Billy over 3 years

    Using WinForms; Is there a better way to find the checked RadioButton for a group? It seems to me that the code below should not be necessary. When you check a different RadioButton then it knows which one to uncheck… so it should know which is checked. How do I pull that information without doing a lot of if statements (or a switch).

         RadioButton rb = null;
    
         if (m_RadioButton1.Checked == true)
         {
            rb = m_RadioButton1;
         }
         else if (m_RadioButton2.Checked == true)
         {
            rb = m_RadioButton2;
         }
         else if (m_RadioButton3.Checked == true)
         {
            rb = m_RadioButton3;
         }
    
  • Admin
    Admin about 13 years
    Should that be s.FirstOrDefault instead of list.FirstOrDefault in the second paragraph of your answer?
  • SLaks
    SLaks about 13 years
    @Phoenix: s is a pluralization, not a variable name.
  • D'Hag
    D'Hag over 11 years
    Excellent answer, but did not really answer OP's main question. See my answer.
  • Frank Tzanabetis
    Frank Tzanabetis about 11 years
    Should be this instead - if (((RadioButton)sender).Checked)
  • Binkan Salaryman
    Binkan Salaryman over 10 years
    it means you could use quickly switch-case's on the selected radiobutton id (using the property System.Windows.Forms.Control.Tag) please dont rate down if you simply dont understand my addition to the question.
  • Murhaf Sousli
    Murhaf Sousli over 10 years
    There's no CheckedChanged in WPF's RadioButton where did you get it from?
  • Mike
    Mike over 8 years
    r.GroupName == "GroupName" and r.IsChecked are of different types (bool and bool?), you should have used r.IsChecked.Value, which is of type bool.
  • JoshYates1980
    JoshYates1980 almost 8 years
    for you vb.net guys, I'll save you a trip to the Telerik converter: Dim checkedButton = radConnections.Controls.OfType(Of RadioButton)().FirstOrDefault(Function(r) r.Checked)
  • Firnas
    Firnas almost 8 years
    try .Where(r => r.GroupName == "GroupName" && r.IsChecked == true) because r.IsChecked is nullable bool property.
  • Unnikrishnan
    Unnikrishnan almost 7 years
    Modifying only the 'FormatableString' result, I was able to use the code in my program. I was also not aware of radioButton Tag. This is how the answers should be. Great Sir.
  • Unnikrishnan
    Unnikrishnan almost 7 years
    what does myGrid refer to here.
  • Bella Swan
    Bella Swan about 5 years
    @Mike Could you tell what is "radioButtons" here?
  • Bella Swan
    Bella Swan about 5 years
    @Firnas Could you tell what is "radioButtons" here?
  • Mike
    Mike about 5 years
    @BellaSwan - I've edited the original answer, it should be clear for you now.
  • Bella Swan
    Bella Swan about 5 years
    @Mike the edits will take time to be approved yet. Could you not explain in the comments?
  • Mike
    Mike about 5 years
    @BellaSwan - have a look at the code - you create a list of all the radio buttons and assign it to a variable. And then, on the same variable you need to run the Where selection and assign it to rbTarget.