在C#中使用radioButtons的groupBox的事件处理程序

use*_*772 13 c# groupbox event-handling radio-button

我在groupBox中有一些radionButtons,我需要做一些我可以称之为"radiobuttons.checked之一"的动作,或者从radiobutton中找出哪些索引被改变了.我试图在事件列表中找到它,但我找不到合适的事件.

编辑: 为了使它更清楚:我需要知道是否存在一些handel我将为goupBox编写处理程序方法而不是单个radioButton.我知道如何使用radiButton.checkedChanged,但它不是我发现的......或者不同我需要知道groupBox在监视这个groupBox中发生了什么的选项 - 我的意思是只有groupBox的处理程序.我发现处理程序"在组框中发生的事情"或者如果存在则相似.

它位于Visual Studio 2012 中的WFA(Windows Presentation Application)中.

Dav*_*New 32

我想你想做的是将所有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
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这不是两次开火吗?一次用于旧选择的单选按钮,一次用于新的?因为两者都改变​​了价值,对吧? (5认同)
  • 当前语法为 `radioButton1.CheckedChanged += radioButtons_CheckedChanged;` 不需要将 new EventHandler 包裹起来,即不需要说 `radioButton1.CheckedChanged += new EventHandler(radioButtons_CheckedChanged);` (2认同)

ada*_*537 6

与 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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Ton*_*son 5

就我所知,没有任何内置的内容.

将tag属性设置为某种指示符(0到n)即可.

添加CheckChangedHandler

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

然后像.

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
  }
} 
Run Code Online (Sandbox Code Playgroud)

如果你有一些这些,我会看一个radiogroup组件.


小智 5

我遇到了同样的问题:一个名为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;
        }
   }
Run Code Online (Sandbox Code Playgroud)


Big*_*ian 2

System.Windows.Forms.RadioButton.CheckedChanged

是您需要的活动

所以做类似的事情:

    public Form1()
    {
        InitializeComponent();

        this.radioButton1.CheckedChanged += new EventHandler(radioButton1_CheckedChanged);
    }

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        // your action
    }
Run Code Online (Sandbox Code Playgroud)