如何在c#,windows中读取radiobutton数组选择的值? [英] how to read radiobutton array selected value in c#,windows?

查看:98
本文介绍了如何在c#,windows中读取radiobutton数组选择的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了带有radiobuttons的动态组合框。现在我有一个问题是使用名称读取groupbox中的选中项目。帮助我







i尝试此代码

radiobutton数组创建:< br $>


 groupbox gp =  new  groupbox(); 
RadioButton [] rb; RadioButton [] rb1;

rb [ 0 ] = new RadioButton(); rb [ 0 ]。Name = rb + < span class =code-digit> 1 ;
rb1 [ 0 ] = new RadioButton(); rb [ 0 ]。Name = rb1 + 1 ;
flowlayoutpanel1.controls。 add (gp);





像这样我创建了10个带有2个radiobuttons的组合框。在每个组框中选择radiobutton后,我想从每个组框中读取选中的项目



按钮点击代码:



 Control [] r1 =  this  .Controls.Find(  rb0 true ); 







现在我如何阅读检查项目?

解决方案

< blockquote>有很多方法可以解决这个问题。我将创建一个Dictionary of Types GroupBox和RadioButton,其中GroupBox是Key,RadioButton是每个KeyValuePair的值:

 //使用'控制类型解释下面$ b的原因$ b private Dictionary <   Control,   控制 >  dctGrpBoxRadBtn; 

//便利变量
private控制rbtCurrent;

您可以在代码中初始化和播种字典:

  private   void  FormTemplate_Load( object  sender,EventArgs e) 
{
dctGrpBoxRadBtn = new 字典<控制,控制>
{
{groupBox1, null }, // 注意'null在这里没问题
{groupBox2, null },
{groupBox3, null },
};

// 你可能已经在表单设计时编辑器中完成了这个
// groupBox1.Controls.Add(radioButton1);
// groupBox1.Controls.Add(radioButton2);

// 分配GroupBox中的所有RadioButtons
// 使用相同的Click EventHandler
radioButton1.Click + = RadioButton_Click;
radioButton2.Click + = RadioButton_Click;
}

在这个简短的例子中,我们只使用两个RadioButtons,两个都添加到一个GroupBox的ControlsCollection中,'groupBox1。



在GroupBox中所有RadioButtons对使用的Click EventHandler中:

  private   void  RadioButton_Click( object  sender,EventArgs e)
{
rbtCurrent = sender as 控制;

if (rbtCurrent == null throw new ArgumentException( RadioButton_Click无效参数);

dctGrpBoxRadBtn [rbtCurrent.Parent] = rbtCurrent;
}

请注意,即使我们将'sender here转换为Type RadioButton,当我们访问RadioButton的'Parent属性时,返回的是Type Control,而不是RadioButton ......那是为什么我们进行字典键类型控制。



所以,现在,每当两个RadioButons中的一个被Clicked时,RadioButton就变成了 Value 在该GroupBox的 Key 字典中。



如果我们想知道为给定的GroupBox选择/检查哪个RadioButton,我们可以使用词典:

  private   void  button1_Click( object  sender,EventArgs e)
{
rbtCurrent = dctGrpBoxRadBtn [groupBox1];

if (rbtCurrent == null )MessageBox.Show( none selected);

if (rbtCurrent == radioButton1) // 请参阅下面的[1]
{
MessageBox.Show( radioButton1选择);
}
else
{
MessageBox.Show( radioButton2选择);
}
}

关于WinForms中RadioButton的一个奇怪的事情是,最初,如果你在一些ContainerControl中有一组,而你还没有设置一个他们作为'检查设计师,然后没有检查;当然,一旦你检查一个,从那时起,其中一个将保持检查......除非...在你的代码中......你取消选中所有这些。



所以你有一种可能的空状态来处理没有检查RadioButtons的地方。



另一个小麻烦是启动WinForms的过程应用程序将触发某些控件的某些事件的触发。



对于RadioButtons,其中一个将触发其Click EventHandler(我相信最后一个添加到父控件中)。



我们使用Type Control而不是特定的类型GroupBox和上面的RadioButton来节省额外的演员。



[1]请注意,我们可以使用RadioButton的特定实例测试RadioButton as Control:RadioButton继承自Control。


i created dynamic groupboxes with radiobuttons in it. now i have a problem to read the checked item in groupbox using name. help me



i tried this code
radiobutton array creation:

groupbox gp=new groupbox();
RadioButton[] rb; RadioButton[] rb1;

rb[0] = new RadioButton();rb[0].Name = "rb" + 1;
rb1[0] = new RadioButton();rb[0].Name = "rb1" + 1;
flowlayoutpanel1.controls.add(gp);



like this i created 10 groupboxes with 2 radiobuttons.After selecting radiobutton in every groupboxes i want to read the checked item from each groupbox

in button click code:

Control[] r1 = this.Controls.Find("rb0", true);




now how can i read the checked items?

解决方案

There are many ways you could approach this. I would create a Dictionary of Types GroupBox and RadioButton, where the GroupBox was the Key, and the RadioButton was the Value, of each KeyValuePair:

// reasons for use of 'Control Type explained below
private Dictionary<Control, Control> dctGrpBoxRadBtn;

// convenience variable
private Control rbtCurrent;

You could initialize and "seed" the Dictionary in your code:

private void FormTemplate_Load(object sender, EventArgs e)
{
    dctGrpBoxRadBtn = new Dictionary<Control, Control>
    {
      { groupBox1, null },  // note that 'null is okay here
      { groupBox2, null },
      { groupBox3, null },
    };

    // you've probably done this in the Form design-time editor
    // groupBox1.Controls.Add(radioButton1);
    // groupBox1.Controls.Add(radioButton2);

    // assign all RadioButtons in GroupBoxes
    // to use the same Click EventHandler
    radioButton1.Click += RadioButton_Click;
    radioButton2.Click += RadioButton_Click;
}

In this brief example we'll just use two RadioButtons, both added to the ControlsCollection of one GroupBox, 'groupBox1.

In the Click EventHandler used by all pairs of RadioButtons in GroupBoxes:

private void RadioButton_Click(object sender, EventArgs e)
{
    rbtCurrent = sender as Control;

    if(rbtCurrent == null) throw new ArgumentException("invalid argument to RadioButton_Click");

    dctGrpBoxRadBtn[rbtCurrent.Parent] = rbtCurrent;
}

Note that even though we cast 'sender here into Type RadioButton, when we access the 'Parent property of the RadioButton, what is returned is of Type Control, not RadioButton ... that's why we made the Dictionary Key Type Control.

So, now, every time one of the two RadioButons is Clicked, that RadioButton becomes the Value in the Dictionary of the Key of that GroupBox.

If we want to know which RadioButton is selected/checked for a given GroupBox, we can just use the Dictionary:

private void button1_Click(object sender, EventArgs e)
{
    rbtCurrent = dctGrpBoxRadBtn[groupBox1];

    if (rbtCurrent == null) MessageBox.Show("none selected");

    if (rbtCurrent == radioButton1) // see [1] below
    {
        MessageBox.Show("radioButton1 selected");
    }
    else
    {
        MessageBox.Show("radioButton2 selected");
    }
}

One of the "odd things" about the RadioButton in WinForms is that initially, if you have a group of them in some ContainerControl, and you have not set one of them as 'Checked in the designer, then none of them are checked; of course, once you check one, from then on one of them will stay checked ... unless ... in your code ... you uncheck all of them.

So you have kind of a possible "null" state to deal with where no RadioButtons are checked.

The other minor annoyance is that the process of starting a WinForms Application will trigger the firing of certain events for certain Controls.

In the case of RadioButtons, one of them is going to have its Click EventHandler fired (the last one added to the Parent Control, I believe).

We used Type Control rather than specific Types GroupBox, and RadioButton above to save extra casting.

[1] Note that we can test a RadioButton as Control with a specific instance of RadioButton: RadioButton inherits from Control.


这篇关于如何在c#,windows中读取radiobutton数组选择的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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