我怎样才能检查“更多”单选按钮? [英] How can i check "more" radiobuttons?

查看:72
本文介绍了我怎样才能检查“更多”单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我有一个问题。如果我在两个组合中都有2个带有radiobutton的组合框,我怎样才能在每个组框中标记一个单选按钮?因为如果我现在这样做我只能有一个..:s

解决方案

请查看CHill60对问题的评论。



问题应该已经解决了:两组的两个独立的组框。您可以为这些单选按钮使用任何类型的父控件,例如面板。



-SA


RadioButtons 使用 Parent 属性来检查排他性。当两个RadioButton共享同一个父节点时,它们不能同时检查(甚至以编程方式),但是当它们具有不同的父节点时,例如当它们被放置在不同的容器(GroupBox,Panels等)中时,它们可以在同时。



但是,如果你只想禁用另一个容器上的按钮,你可以操纵 Checked 以编程方式实现此属性。像这样,



 RadioButton_checkChanged( Object  sender,EventArgs e)
{
if (RadioButton1.Checked)
RadioButton2.Checked = false ;
}





如果有太多的RadioButton需要操作,这种方法可能会变得乏味。这是我之前用过的其他方法,仅修改它以使用RadioButton的标签属性来检查或取消选中RadioButton。



您可以下载源这里。



  //  首先,我们希望能够在表格和所有容器中找到所有RadioButtons。 
// < span class =code-comment>然后将RadioButtons添加到Collection中,以便以后可以对它们进行操作。
List< radiobuttons> AllRadios = new List< radiobuttons>();
void FindRadios(控件容器)
{
foreach (控制x container.Controls) // 遍历所有容器中的控件
{
if (x.HasChildren) // 此Control是否是包含其他控件的容器?
{
FindRadios(x); // 查找容器上的所有控件
}

< span class =code-keyword> if (x RadioButton) // < span class =code-comment>这个控件是RadioButton吗?
{
AllRadios.Add(x as RadioButton) ; // 将RadioButton添加到列表中。
((RadioButton)x)。Click + = BindRadiosEvent; // 将RadioButton的Click事件绑定到一个。
}
}

}
// 现在,Checked Property将在使用每个RadioButton的Tag属性的BindRadiosEvent。
void BindRadiosEvent( object sender,EventArgs e)
{
RadioButton clicked = sender as RadioButton;
if (clicked.Tag == null 返回; // 如果RadioButton的Tag属性为空,请离开。
AllRadios.ForEach( // 查找具有相同标记的所有RadioButtons。
委托(RadioButton x)
{
如果(x!=点击&& x.Tag ==点击.Tag)
{
x.Checked = false ;
clicked.Checked = true ;
}
}
);
}





现在,通过将两个RadioButtons的Tag属性设置为相同的东西。他们将表现得好像是在同一个容器上。



突破ILSpy以了解微软是如何做到的。


Hey guys, i have a question. If i have 2 groupboxes with radiobuttons in both, how can i make that i can mark one radiobutton in each groupbox? Cause if i now do that i can only have one.. :s

解决方案

Please see the comment to the question by CHill60.

The problem should be already solved: two separate group boxes for the two groups. You can use any class of parent controls for those radio buttons, such as panels.

—SA


RadioButtons use the Parent property to check for exclusivity. When two RadioButtons share the same parent, they cannot both be checked at the same time (even programmatically) but when they have different Parents, like when they are placed in different containers (GroupBoxes, Panels, etc), they can be checked at the same time.

However, if you just wanted to disable a button on another container, you could manipulate the Checked Property programmatically to achieve this. Like so,

RadioButton_checkChanged(Object sender, EventArgs e)
{
    if (RadioButton1.Checked)
        RadioButton2.Checked = false;
}



This method however could get tedious if there are so many RadioButtons to manipulate. This is a workaround I used sometime ago for something else, only just modified it to use the Tag Property of the RadioButton to check or uncheck a RadioButton.

You can download the source here.

//First we want to be able to find all RadioButtons on a Form and in all containers.
//The RadioButtons will then be added to a Collection so that they can be manipulated later.
List<radiobuttons> AllRadios = new List<radiobuttons>();
void FindRadios(Control container)
        {
            foreach (Control x in container.Controls) //Iterate through all the Controls in the Container
            {
                if (x.HasChildren) //Is this Control a container containing other Controls?
                {
                    FindRadios(x); //Find all Controls on the Container
                }

                if (x is RadioButton) //Is this Control a RadioButton?
                {
                    AllRadios.Add(x as RadioButton); //Add the RadioButton to the list.
                    ((RadioButton)x).Click += BindRadiosEvent; //Bind the Click event of the RadioButton to a single one.
                }
            }
            
        }
// Now, the Checked Property will be compared in a BindRadiosEvent using the Tag Property of each RadioButton.
void BindRadiosEvent(object sender, EventArgs e)
        {
            RadioButton clicked = sender as RadioButton;
            if (clicked.Tag == null) return; //If the Tag property of the RadioButton is blank, leave.
            AllRadios.ForEach( //Find all RadioButtons with the same tag.
                    delegate(RadioButton x)
                    {
                        if (x != clicked && x.Tag == clicked.Tag)
                        {                                                        
                            x.Checked = false;
                            clicked.Checked = true;
                        }
                    }
                );
        }



Now, by setting the Tag property of two RadioButtons to the same thing. They will act as if they were on the same Container.

Breakout ILSpy to see how Microsoft did theirs.


这篇关于我怎样才能检查“更多”单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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