Event handler for groupBox with radioButtons in C #

I have a few radionButtons buttons in a groupBox, and I need to do an action that I could call "one of the radioobuttons.checked changed", or find out from what number the index changed. I tried to find it in the event list, but I could not find the correct one.

Edit: To make this clearer: I need to know if there is any handel for me to write a handler method for goupBox, but not for one radioButton. I know how to use radiButton.checkedChanged, but that is not what I find. Or in another way I need to know what options groupBox have to monitor what is happening inside this groupBox - I mean only handlers for groupBox. I find the handler "something happens in the group field" or similar, if any.

This is in WFA (Windows Presentation Application) in Visual Studio 2012.

+9
source share
10 answers

I think you want to bind all RadioButtons CheckedChanged events 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 } } 
+25
source

Nothing has been created for this, as far as I know.

Set the tag property to some indicator (from 0 to n).

Add CheckChangedHandler

Direct all buttons to CheckChanged events.

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 have several of them, I would look at the radio group component.

+4
source

I had the same problem: a group box named Button Type (gbxButtonType) with 6 radio buttons and another group field named " Character Type" (gbxIconType) with 8 radio buttons. When a user selects one radio button from each group field, a MessageBox with the selected selection after pressing the DisplayButton button. My problem was that there were no CheckedChanged events in group mailboxes. AKN's solution worked fine:

 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; } } 
+2
source

System.Windows.Forms.RadioButton.CheckedChanged

is the event you need

So do something like:

  public Form1() { InitializeComponent(); this.radioButton1.CheckedChanged += new EventHandler(radioButton1_CheckedChanged); } private void radioButton1_CheckedChanged(object sender, EventArgs e) { // your action } 
+1
source

I think that you want to handle the selection of some of the switches inside the group box using the group core control itself .

Perhaps you need to basically avoid code repetition.

(i.e.) adding a check change event for the entire switch in the designer, which can be tedious when there is more control. Since it is already in the group, why not use the group control object to control the controls with it and set events.

This is how I understood your problem and therefore the solution as below.

Set a common handler for all radio button controls in the group field

 for (int i = 0; i < groupBox.Controls.Count; i++) { RadioButton rb = (RadioButton)groupBox.Controls[i]; rb.CheckedChanged += new System.EventHandler(evntHandler); } 

Inside the handler, you can determine which button has been changed, as indicated by others, and perform the necessary actions.

+1
source

// Here you are kindly providing Jock Frank Halliday

  //^subscribe events to radio button check changed private void seriesTxtBxEvent() { //Show txtBx this.radBtn_RoomSeries.CheckedChanged += new EventHandler(showSeriesTxtBx_Event); //Hide txtBx this.radBtn_RoomNumber.CheckedChanged += new EventHandler(hideSeriesTxtBx_Event); this.radBtn_RoomName.CheckedChanged += new EventHandler(hideSeriesTxtBx_Event); this.radBtn_RoomLevel.CheckedChanged += new EventHandler(hideSeriesTxtBx_Event); this.radBtn_RoomDep.CheckedChanged += new EventHandler(hideSeriesTxtBx_Event); } private void hideSeriesTxtBx_Event(object sender, EventArgs e) { tbx_SheetSeries.Visible = false; } private void showSeriesTxtBx_Event(object sender, EventArgs e) { tbx_SheetSeries.Visible = true; } 
+1
source
 //Form Start void MainFormLoad(object sender, EventArgs e) { Control.ControlCollection locais = groupBoxLocalização.Controls; foreach (CheckBox chkBox in locais) { chkBox.MouseUp += chkBoxLocais_MouseUp; } } // Event void chkBoxLocais_MouseUp(object sender, MouseEventArgs e) { //Tratar individualmente CheckBox chk = (CheckBox)sender; //ou para tratar todos objetos de uma vez Control.ControlCollection locais = groupBoxLocalização.Controls; foreach (CheckBox chkBox in locais) { //chkBox.... } } 
0
source

You may be able to do this with a timer, but this is just bad for optimization, the simple solution is that for each radio exchange you just add only one function as a ChekedChanged event.

0
source

Groupbox will limit only one radio button

So Setp1: you can assign one CheckedChanged event handler to all the switches

 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 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; } } } 
0
source

Similar to davenewza's answer (and probably should have had a comment, but I don't have enough reputation), but the event fires 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 } } } 
0
source