Which Radio button in the group is checked?
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.
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, 2020Comments
-
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 about 13 yearsShould that be
s.FirstOrDefault
instead oflist.FirstOrDefault
in the second paragraph of your answer? -
SLaks about 13 years@Phoenix:
s
is a pluralization, not a variable name. -
D'Hag over 11 yearsExcellent answer, but did not really answer OP's main question. See my answer.
-
Frank Tzanabetis about 11 yearsShould be this instead - if (((RadioButton)sender).Checked)
-
Binkan Salaryman over 10 yearsit 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 over 10 yearsThere's no CheckedChanged in WPF's RadioButton where did you get it from?
-
Mike over 8 yearsr.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 almost 8 yearsfor 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 almost 8 yearstry
.Where(r => r.GroupName == "GroupName" && r.IsChecked == true)
becauser.IsChecked
is nullable bool property. -
Unnikrishnan almost 7 yearsModifying 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 almost 7 yearswhat does myGrid refer to here.
-
Bella Swan about 5 years@Mike Could you tell what is "radioButtons" here?
-
Bella Swan about 5 years@Firnas Could you tell what is "radioButtons" here?
-
Mike about 5 years@BellaSwan - I've edited the original answer, it should be clear for you now.
-
Bella Swan about 5 years@Mike the edits will take time to be approved yet. Could you not explain in the comments?
-
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.