How do I get which radio button is checked from a groupbox?
Solution 1
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.
Solution 2
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🍻
Solution 3
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);
Amit Bisht
MyExperience Ex; foreach(Day day in MyLife) { Ex = new Experiences(); day.Add(Ex); }
Updated on March 22, 2020Comments
-
Amit Bisht over 4 years
I have these groupboxes:
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?
-
Tot Zam almost 9 yearspossible duplicate of Which Radio button in the group is checked?
-
-
Amit Bisht almost 11 yearsi am using windows forms and there is no option of radio group
-
César Amorim almost 10 years@Soner I tried to use this code, but it provides a error of ambiguity between ´Forms.RadioButton´ and ´Controls.RadioButton´ , what does it means?
-
Soner Gönül almost 10 years@CésarAmorim Both
System.Windows.Controls
andSystem.Windows.Forms
haveRadioButton
class. I feel like, in your project both these namepsaces are added and compiler confuses which namespace belongs on thisRadioButton
when you type it only it's name. As a solution, you can use it's full name to prevent this situation likeSystem.Windows.Controls.RadioButton
orSystem.Windows.Forms.RadioButton
. -
Ignas Vyšnia almost 9 yearsHad some trouble following this for Universal Windows Apps, as
Windows.UI.Xaml.Controls.Page
doesn't haveControls
property there, so it's best to use some encapsulating WPF element, like namedStackPanel
orGrid
andChildren
property:rbPanel.Children.OfType<RadioButton>().FirstOrDefault(r => r.IsCheked.Value);
(IsChecked
is nullable, hence the.Value
) -
Paul Alexander over 7 yearsWould someone be able to explain the .FirstOrDefault(n => n.Checked); line for me please?
-
Soner Gönül almost 4 years@PaulAlexander
OfType<RadioButton>
method returnsIEnumerable<RadioButton>
which means an iterable collections ofRadioButton
's.FirstOrDefault
method returns the first element (ordefault
if can't find any element satisfied that condition) which satisfied with given condition, which isn.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, checkEnumerable
class from MSDN: docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable