I have a group of 8 radiobuttons, is there a simple way to check if "checked" status has changed for the group? I don't need to know which button has been checked, just if a different button has been checked. Sort of like a CheckedChanged event for the whole group.
Asked
Active
Viewed 2,056 times
1
-
No, but it easy to implement a simple variable that is set to false after the initial load and set to true if any of the radiobuttons fire the CheckedChanged event – Steve Oct 08 '16 at 12:08
-
3Hanlde `CheckedChanged` of all radio buttons using a single method. – Reza Aghaei Oct 08 '16 at 12:08
-
try using if(radioButton1.checked) and in aspx file fire CheckChanged event.. This question is Same as http://stackoverflow.com/questions/1797907/which-radio-button-in-the-group-is-checked?rq=1 – sonsha Oct 08 '16 at 12:12
3 Answers
5
You can assign the same CheckedChanged
event handler to all of the radiobuttons. When you check a radiobutton, the method will be called twice (for the radiobutton losing the checkmark and for the radiobutton being checked). So only handle the event for the one that is checked.
private void anyRadioButton_CheckedChanged(object sender, EventArgs e)
{
// The radio button that raised the event
var radioButton = sender as RadioButton;
// Only do something when the event was raised by the radiobutton
// being checked, so we don't do this twice.
if(radioButton.Checked)
{
// Do something here
}
}

NineBerry
- 26,306
- 3
- 62
- 93
-
I think this is what I was looking for. Thanks to everyone for your suggestions! – Rado Oct 08 '16 at 12:28
2
I think what you are looking for is this, a common handler for all radio buttons in the group
radioButton1.CheckedChanged += new EventHandler(radioButtons_CheckedChanged);
radioButton2.CheckedChanged += new EventHandler(radioButtons_CheckedChanged);

Miguel Sanchez
- 434
- 3
- 11
2
You can wire up all of the RadioButtons' CheckedChanged event to the same handler. Follow this code.
public Form1()
{
rB1.CheckedChanged += new EventHandler(rB_CheckedChanged);
rB2.CheckedChanged += new EventHandler(rB_CheckedChanged);
}
private void rB_CheckedChanged (object sender, EventArgs e)
{
RadioButton radioButton = sender as RadioButton;
if (rB1.Checked)
{
}
else if (rB2.Checked)
{
}
}

M. Wiśnicki
- 6,094
- 3
- 23
- 28