确定在组框中选中了哪个单选按钮. [英] to determine which radio button is checked in group box.

查看:73
本文介绍了确定在组框中选中了哪个单选按钮.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个组合框中有7个单选按钮.我有以下代码

I have 7 radio buttons in a group box. I have the following code

private void initgrpBox1()
{
  groupBox1.Controls.Add(radioButton1);
  groupBox1.Controls.Add(radioButton2);
  groupBox1.Controls.Add(radioButton3);
  groupBox1.Controls.Add(radioButton4);
  groupBox1.Controls.Add(radioButton5);
  groupBox1.Controls.Add(radioButton6);
  groupBox1.Controls.Add(radioButton7);
  Controls.Add(groupBox1);
}


我想为组框中选中的每个单选按钮设置一个字母.
我尝试使用foreach语句遍历groupbox中的单选按钮,但是它不起作用


I want to set a alphabet for each radio button checked in the group box.
I tried using foreach statement to traverse through the radiobuttons in groupbox,but it''s not working

//foreach(rad as RadioButton in groupBox1.controls )


请让我知道如何检查单选按钮是否已选中.


Please let me know how to check whether a radio button in checked.

推荐答案

让我们假设这是System.Windows.Forms.

创建一些您需要的数据类型,以识别与单选按钮相关的操作.假设这是一个表示某些选项集的枚举,但可以是任何值:

Let''s assume this is System.Windows.Forms.

Create some data type you need for identification of the action related to the radio button. Let''s assume this is a enumeration expressing some option set, but it can be anything:

enum RadioOption {
   None, Good, Bad, Fair, Excellent, Perfect,
}



现在,将此数据添加为Control.Tag(类型为System.Object:



Now, add this data as a Control.Tag (which is of the type System.Object:

RadioButton rb = new Radio(button);
rb.Tag = RadioOption.Good;
rb.Name = string.Format("&{0}", RadioOption.Good); 
rb.Top = //...
//...
groupBox1.Controls.Add(rb);
rb = new Radio(button);
rb.Tag = RadioOption.Perfect;
rb.Name = string.Format("&{0}", RadioOption.Perfect); 
rb.Top = //...
//...
groupBox1.Controls.Add(rb);



现在,您需要从选定的单选按钮中提取此信息:



Now, you need to extract this information from a selected radio button:

static RadioOption GetOption(RadioButton radio) {
    return (RadioOption)radio.Tag;
}



您应确保仅使用上述方法调用标记"单选按钮的上述方法.您可以在标签中放入其他任何信息.根据您的使用方式定义所需的类型.

事件RadioButton.CheckedChanged很好地使用了它.
为每个人执行以下操作:



You should make sure that the method above is only called for the radio buttons "tagged" using the method shown above. You can put any other information in the tag. Define the type you need, depending on how you want to use it.

A very good use of it is the event RadioButton.CheckedChanged.
For each of them do the following:

rb.CheckedChanged += (sender, eventArgs) => {
   RadioButton aButton = (RadioButton)sender;
   RadioOption option = GetOption(sender as RadioButton);
   DoSomethingWhenCheckedChanged(aButton.Cheched, option);
}; //rb.CheckedChanged



—SA



—SA


只需在单个事件处理程序中处理组中所有单选按钮的Clicked事件,然后将单击的单选按钮存储为单独的变量,就像这样:

Just handle the Clicked event for all of the radio buttons in the group in a single event handler, and store the clicked radio button as a separate var, something like this:

public partial class MyForm
{
    RadioButton m_radiogroup1Checked = null;

    private void initgrpBox1()
    {
        AddRadioToGroupBox(groupBox1, radioButton1);
        AddRadioToGroupBox(groupBox1, radioButton2);
        AddRadioToGroupBox(groupBox1, radioButton3);
        AddRadioToGroupBox(groupBox1, radioButton4);
        AddRadioToGroupBox(groupBox1, radioButton5);
        AddRadioToGroupBox(groupBox1, radioButton6);
        AddRadioToGroupBox(groupBox1, radioButton7);
        Controls.Add(groupBox1);
    }

    private void AddRadioToGroupBox(GroupBox groupBox, RadioButton radioButton)
    {
        groupBox.Controls.Add(radioButton);
        radioButton.Clicked += new EventHandler(radioGroup1Checked);
    }

    private void radioGroup1_Clicked(object sender, e EventArgs)
    {
        m_radiogroup1Checked = sender as RadioButton;
    }
}



这样,几乎没有太多的额外代码编写(如其他解决方案所示).只需让表单执行其操作,您就可以随时查看m_radiogroup1Checked的值以查看选中了哪个按钮.

容易撒尿,柠檬榨汁...

您还可以创建一个RadioGroup类,为您处理所有这些事情...



This way, there''s not nearly as much extra code write (as shown in the other solutions). Just let the form do its thing, and any time you want, you can check the value of m_radiogroup1Checked to see which button was checked.

Easy peesy, lemon squeezy...

You could also create a RadioGroup class that handled all this stuff for you...


您可以尝试此

You could try this

foreach (RadioButton rb in groupBox1.Controls)
           {
               MessageBox.Show(rb.Name);
           }



但是,如果您的GroupBox
中有比RadioButton更多的控件,则可能会中断
所以你最好用这个:



But this would break if there are ohter controls than RadioButton in your GroupBox

So you better use this:

foreach (Control c in groupBox1.Controls)
            {
                if (c.GetType() == typeof(RadioButton))
                {
                    RadioButton rb = c as RadioButton;
                    if (rb.Checked)
                    {
                        MessageBox.Show(rb.Name); 
                    }
                }
            }
            }


这篇关于确定在组框中选中了哪个单选按钮.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆