How do I get which radio button is checked from a groupbox?
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);
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.
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ð»