Event handler for groupBox with radioButtons in C#
Asked Answered
I

12

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.

Iso answered 22/6, 2012 at 16:44 Comment(4)
You might want to start with WPF in the question subject, or in the tags. Also tagged incorrectly.Rashida
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).Iso
Also there could be only 5 tags under the question ...Iso
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 agoRashida
I
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
    }
}
Illuminative answered 22/6, 2012 at 16:56 Comment(4)
Btw, I am assuming that you are using WinForms.Illuminative
Doesn't this fire the event twice? Once for the old selected radio button and once for the new? because both changed value, right?Algy
@Algy 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 methodFarceuse
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);Farceuse
L
6

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
        }
    }
}
Labiche answered 21/12, 2018 at 18:14 Comment(0)
A
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.

Angel answered 22/6, 2012 at 17:2 Comment(2)
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.Iso
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.Angel
T
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;
        }
   }
Tripetalous answered 17/10, 2013 at 13:34 Comment(0)
D
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;
        }
    }
}
Dyna answered 28/2, 2018 at 4:26 Comment(0)
R
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
    }
Rashida answered 22/6, 2012 at 16:55 Comment(2)
assuming you are using WinForms, and the standard out of the box controls, and also assuming that I am interpreting your question correctlyRashida
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 ...Iso
W
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;
    }
Wiser answered 12/1, 2016 at 11:11 Comment(0)
R
0

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.

Roid answered 13/3, 2013 at 13:42 Comment(0)
F
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....
    }

}
Factory answered 13/2, 2017 at 14:52 Comment(0)
P
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.

Pinsk answered 21/2, 2018 at 10:46 Comment(0)
D
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
}
Dumbarton answered 11/12, 2020 at 15:56 Comment(0)
B
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.

Burcham answered 20/7, 2021 at 3:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.