C# 中带有单选按钮的 groupBox 的事件处理程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11160594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:48:48  来源:igfitidea点击:

Event handler for groupBox with radioButtons in C#

c#event-handlingradio-buttongroupbox

提问by user1097772

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.

我在 groupBox 中有一些 radioonButtons,我需要做一些我可以称之为“其中一个 radiobuttons.checked 更改”的操作,或者从 radiobutton 中找出更改了什么索引。我试图在事件列表中找到它,但我找不到合适的。

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.

编辑:为了更清楚:我需要知道是否存在一些关于我将为goupBox而不是单个radioButton编写处理程序方法的handel。我知道如何使用 radiButton.checkedChanged,但这不是我所发现的。我发现处理程序“在组框中发生了一些事情”或类似的(如果存在)。

It's in WFA (Windows Presentation Application)in Visual studio 2012.

它位于 Visual Studio 2012 的WFA(Windows 演示应用程序)中。

回答by Big Endian

System.Windows.Forms.RadioButton.CheckedChanged

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
    }

回答by Dave New

I think what you want to do is wire up all of the RadioButtons' CheckedChanged event to the same handler.

我认为您想要做的是将所有 RadioButtons 的 CheckedChanged 事件连接到同一个处理程序。

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

回答by Tony Hopkinson

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.

将标签属性设置为某种指标(0 到 n)即可。

Add a CheckChangedHandler

添加一个 CheckChangedHandler

Point all the buttons CheckChanged events at it.

将所有按钮 CheckChanged 事件指向它。

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.

如果您有其中的一些,我会查看 radiogroup 组件。

回答by AKN

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

我认为您想使用 groupbox 控件本身来处理 groupbox 内某些单选按钮的选择。

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.

在处理程序中,您可以根据其他人的指示确定哪个按钮已更改并执行必要的操作。

回答by Emanuel

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:

我遇到了同样的问题:一个名为Button Type (gbxButtonType)的组框有 6 个单选按钮,另一个名为Icon Type (gbxIconType) 的组框有 8 个单选按钮。当用户从每个组框中选择一个单选按钮时,单击DisplayButton后将出现一个 MessageBox 并应用该选择。我的问题是组框没有 CheckedChanged 事件。AKN的解决方案完美运行:

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

回答by jock spanker McHalliday

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

回答by Paulo César Ferreira

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

}

回答by János Márk Kirsch

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.

您也许可以使用 Timer 来实现,但这对于优化来说是不利的,简单的解决方案是对于每个单选按钮,您只需添加一个函数作为 ChekedChanged 事件。

回答by Li Kevin

Groupbox will limit only one radio button checked

Groupbox 将限制只选中一个单选按钮

So Setp1:you can assign one "CheckedChanged" event handler to all you radio button

所以Setp1:您可以为所有单选按钮分配一个“CheckedChanged”事件处理程序

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)

Setp2:像这样实现这个事件处理程序(按单选按钮的文本过滤)

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

回答by adamj537

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.

类似于 davenewza 的回答(可能应该是评论,但我没有足够的声誉),但是对于整个单选按钮组,该事件仅触发一次。

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