Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Radio button in the group is checked?

People also ask

What is checked in radio button?

The checked property sets or returns the checked state of a radio button. This property reflects the HTML checked attribute.

What is button group in radio button?

A radio button group is a container control that holds radio buttons. The radio button group defines the layout for the buttons it contains, including a group title, text alignment for button labels, and whether or not to show a border. You can place a radio button group control within a section control.

How do I know which radio is selected?

To check which radio button is selected in a form, we first get the desired input group with the type of input as an option and then the value of this selection can then be accessed by the val() method. This returns the name of the option that is currently selected.

How do you check if a radio button has been selected?

Use document. getElementById('id'). checked method to check whether the element with selected id is check or not.


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).


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

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

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.)


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