user1097772
user1097772

Reputation: 3529

Event handler for groupBox with radioButtons in C#

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.

Upvotes: 17

Views: 74113

Answers (12)

Raheel Zahid
Raheel Zahid

Reputation: 41

  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.

Upvotes: 0

ralofpatel
ralofpatel

Reputation: 59

  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
}

Upvotes: 0

adamj537
adamj537

Reputation: 853

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
        }
    }
}

Upvotes: 7

Li Kevin
Li Kevin

Reputation: 83

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

Upvotes: 3

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.

Upvotes: 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....
    }

}

Upvotes: 0

//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;
    }

Upvotes: 1

Emanuel
Emanuel

Reputation: 51

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

Upvotes: 5

Akaanthan Ccoder
Akaanthan Ccoder

Reputation: 2179

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.

Upvotes: 0

Tony Hopkinson
Tony Hopkinson

Reputation: 20320

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.

Upvotes: 5

Dave New
Dave New

Reputation: 40002

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
    }
}

Upvotes: 43

Big Endian
Big Endian

Reputation: 954

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
    }

Upvotes: 2

Related Questions