Event handler for groupBox with radioButtons in C#
Solution 1
I think what you want to do is wire up all of the RadioButtons' CheckedChanged event to the same handler.
public Form1()
{
radioButton1.CheckedChanged += new EventHandler(radioButtons_CheckedChanged);
radioButton2.CheckedChanged += new EventHandler(radioButtons_CheckedChanged);
// ...
}
private void radioButtons_CheckedChanged (object sender, EventArgs e)
{
RadioButton radioButton = sender as RadioButton;
if (radioButton1.Checked)
{
// Do stuff
}
else if (radioButton2.Checked)
{
// Do other stuff
}
}
Solution 2
I had the same problem: a group box named Button Type (gbxButtonType) with 6 radio buttons and another group box named Icon Type (gbxIconType) with 8 radio button. When the user selected one radio button from each group box, a MessageBox will appear with the selection applied after clicking the DisplayButton. My problem was that the group boxes didn't have a CheckedChanged event. The solution of AKN worked perfectly:
public Form1()
{
InitializeComponent();
for (int i = 0; i < gbxButtonType.Controls.Count; i++)
{
RadioButton rdb = (RadioButton)gbxButtonType.Controls[i];
rdb.CheckedChanged += new System.EventHandler(gbxButtonType_CheckedChanged);
}
for (int i = 0; i < gbxIconType.Controls.Count; i++)
{
RadioButton rdb = (RadioButton)gbxIconType.Controls[i];
rdb.CheckedChanged += new System.EventHandler(gbxIconType_CheckedChanged);
}
}
private void gbxIconType_CheckedChanged(object sender, EventArgs e)
{
if (sender == rdbAsterisk)
{
iconType = MessageBoxIcon.Asterisk;
}
else if (sender == rdbError)
{
iconType = MessageBoxIcon.Error;
}
...
else
{
iconType = MessageBoxIcon.Warning;
}
}
Solution 3
Nothing built in for that as far as I'm aware.
Set the tag property to some sort of indicator (0 to n) will do.
Add a CheckChangedHandler
Point all the buttons CheckChanged events at it.
then something like.
private void radioButtons_CheckedChanged (object sender, EventArgs e)
{
RadioButton radioButton = sender as RadioButton;
int buttonid = (int)radioButton.Tag;
switch (buttonid)
{
case 0 : // do something; break
}
}
If you've got a few of these I'd look at a radiogroup component.
Solution 4
Similar to davenewza's answer (and likely should have been a comment, but I have insufficient reputation), but with the event firing only once for the entire group of radio buttons.
public Form1()
{
// Add a "CheckedChanged" event handler for each radio button.
// Ensure that all radio buttons are in the same groupbox control.
radioButton1.CheckedChanged += new EventHandler(radioButtons_CheckedChanged);
radioButton2.CheckedChanged += new EventHandler(radioButtons_CheckedChanged);
}
private void radioButtons_CheckedChanged (object sender, EventArgs e)
{
// Do stuff only if the radio button is checked (or the action will run twice).
if (((RadioButton)sender).Checked)
{
if (((RadioButton)sender) == radioButton1)
{
// Do stuff
}
else if (((RadioButton)sender) == radioButton2)
{
// Do other stuff
}
}
}
Solution 5
Groupbox will limit only one radio button checked
So Setp1: you can assign one "CheckedChanged" event handler to all you radio button
private void initRadio()
{
radio_button1.CheckedChanged += Radio_show_CheckedChanged;
radio_button2.CheckedChanged +=Radio_show_CheckedChanged;
}
And Setp2: implement this event handler like this (Filter by Radio Button's Text)
private void Radio_show_CheckedChanged(object sender, EventArgs e)
{
RadioButton radioButton = sender as RadioButton;
if (radioButton.Checked == true) { //limited only checked button do function
switch (radioButton.Text)
{
case "name1":
// do your stuff ...
break;
case "name2":
// do your stuff ...
break;
}
}
}
Comments
-
user1097772 almost 3 years
I have some radionButtons in groupBox and I need to do action what I could call "one of radiobuttons.checked changed" or find out from radiobutton what index is changed. I've tryed to find it in list of events but I couldn't find the right one.
Edit: To make it more clear: I need to know if exist some handel for what I'll write handler method for the goupBox not for single radioButton. I know how to use radiButton.checkedChanged, but it's not what I'm finding .. Or differently I need to know what options have the groupBox in monitoring what happens inside this groupBox - I mean only the handlers for the groupBox. I'm finding handler "in the group box is something happens" or simimilar if any exist.
It's in WFA (Windows Presentation Application) in Visual studio 2012.
-
Big Endian about 12 yearsassuming you are using WinForms, and the standard out of the box controls, and also assuming that I am interpreting your question correctly
-
user1097772 about 12 yearsYes I'm using WPF, no you aren't but thanks. I'm already using the checked changed for single radio button. I need to know if exist something like "value of some object in groupBox is changed" - I mean event handler for groupBox not for radioButtons ... Maybe it doesn't ...
-
Dave New about 12 yearsBtw, I am assuming that you are using WinForms.
-
user1097772 about 12 yearsThanks but this my current solution. But there is no radioGroup component, there is only group box in the list of components. So I'm finding some event handler for groupBox.
-
Tony Hopkinson about 12 yearsThere isn't one. All groupbox does is contain other components with a border and caption. I could give you some ideas on how to create a radiogroup compoent, or extend group box. Can't come up with a behaviour that doesn't exist though.
-
Enrico over 8 yearsDoesn't this fire the event twice? Once for the old selected radio button and once for the new? because both changed value, right?
-
barlop over 5 years@Enrico I just tested it and yes it does. So maybe if you don't want that to happen, the Click event is better. With a test to see that the now checked radio button differs from the previously selected one. Also, click runs once automatically when the form loads(not just on click!) but it happens before the form is active. So inside the click method you could test if the form has once been made activated(have a boolean set to true when the form is activated) and within the click method, having tested the boolean for true, then run the code you want for the click method
-
barlop over 5 yearscurrent syntax would be
radioButton1.CheckedChanged += radioButtons_CheckedChanged;
no need to wrap the new EventHandler around it i.e. no need to sayradioButton1.CheckedChanged += new EventHandler(radioButtons_CheckedChanged);