Event handler for groupBox with radioButtons in C#

可紊 提交于 2020-01-12 13:05:27

问题


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.


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



回答2:


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.




回答3:


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



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



回答5:


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
    }



回答6:


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.




回答7:


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



回答8:


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



回答9:


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

}



回答10:


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.



来源:https://stackoverflow.com/questions/11160594/event-handler-for-groupbox-with-radiobuttons-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!