17

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.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
user1097772
  • 3,499
  • 15
  • 59
  • 95
  • You might want to start with WPF in the question subject, or in the tags. Also tagged incorrectly. – Big Endian Jun 22 '12 at 17:15
  • Exept it's not WPF but WFA, why it's so important to mention it? It's not clear enought if I wrote that is in C# there are some diference in event handling in C#? I suppose that if it's one programming language it should be same (It's seems logical for me and I have no information it isn't - but I also don't have any that it is, so I'm asking you because you had problem with it). – user1097772 Jun 22 '12 at 17:24
  • Also there could be only 5 tags under the question ... – user1097772 Jun 22 '12 at 17:27
  • Yes 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 ... – user1097772 3 hours ago – Big Endian Jun 22 '12 at 20:29

12 Answers12

43

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
    }
}
Dave New
  • 38,496
  • 59
  • 215
  • 394
  • 1
    Btw, I am assuming that you are using WinForms. – Dave New Jun 22 '12 at 17:11
  • 10
    Doesn't this fire the event twice? Once for the old selected radio button and once for the new? because both changed value, right? – Enrico Feb 11 '16 at 15:43
  • 1
    @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 Jan 17 '19 at 03:49
  • 2
    current syntax would be `radioButton1.CheckedChanged += radioButtons_CheckedChanged;` no need to wrap the new EventHandler around it i.e. no need to say `radioButton1.CheckedChanged += new EventHandler(radioButtons_CheckedChanged);` – barlop Jan 17 '19 at 06:59
5

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.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
  • Thanks 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. – user1097772 Jun 22 '12 at 17:38
  • There 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. – Tony Hopkinson Jun 22 '12 at 21:50
5

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;
        }
   }
Emanuel
  • 51
  • 1
  • 3
5

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
        }
    }
}
adamj537
  • 677
  • 7
  • 14
3

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;
        }
    }
}
Li Kevin
  • 83
  • 1
  • 8
2

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
    }
Big Endian
  • 944
  • 1
  • 6
  • 23
  • assuming you are using WinForms, and the standard out of the box controls, and also assuming that I am interpreting your question correctly – Big Endian Jun 22 '12 at 16:56
  • Yes 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 ... – user1097772 Jun 22 '12 at 17:10
1

I think your want to handle the selection of some radio buttons inside a groupbox using the groupbox control itself.

May be you wanted this basically to avoid code repetition.

(i.e) adding check change event for all the radio button in the designer which may be tedious when there are more control. Since its already present under a group, why not use the group control object to manipulate controls with-in it and set the events.

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

Set a common handler for all radio button control in the group box

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 was changed as indicated by others and do the necessary action.

Akaanthan Ccoder
  • 2,159
  • 5
  • 21
  • 37
1

//Here you go courtesy of 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;
    }
0
//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....
    }

}
Izuka
  • 2,572
  • 5
  • 29
  • 34
0

You can maybe do it with Timer, but that's just bad for optimalization, the easy solution is that for every radiobutton you simply add only one function as ChekedChanged event.

0
  1. Create a Checked event by double clicking on any of the radio buttons, copy the name of the method that Visual Studio creates for the event
  2. Go to all radio buttons and change the event to the copied one from Properties Explorer > Events Section
  3. In the generated method use the following code. This would fire event for all radio buttons but only "Do your thing" once

Code:

private void radioButtons_CheckedChanged (object sender, EventArgs e)
{
    RadioButton rb = sender as RadioButton;

    if (rb.Checked == false) return;

    // Do your thing
}
ralofpatel
  • 49
  • 3
0
  1. Create Event Checked_Changed on one radio button from Designer Events list.

  2. Add same event to each radio Button from dropdown in front of Checked_Changed event of each radio

  3. inside checked changed event use

    private void CheckedChanged(object sender,EventArgs e)
    {
     var radio = groupBox.Controls.OfType<RadioButton>                  
                ().FirstOrDefault(r => r.Checked).Name;
    }
    

you can get which radio is active now.