Pop*_*oko 3 .net c# controls radio-button winforms
我有一个包含几个单选按钮的表单,这些单选按钮假设代表不同的整数值。
1)是否有内置的方法可以将值放在单选按钮“后面”?一个例子:
RadioButton rad = new RadioButton();
rad.Value = 5; // This is what im trying to achieve.
2)我有几个单选按钮在逻辑上彼此相关,我想知道选择了哪个(不将它们放在 GroupBox 控件中)并获取该单选按钮的选定值。注意:我想“迭代”这 3 个控件并获取选定的控件,而不是执行 3 个 IF 语句。一个例子:
RadioButton radColor1 = new RadioButton();
RadioButton radColor2 = new RadioButton();
RadioButton radColor3 = new RadioButton();
// ...
RadioButton radSelected = ?? // Get selected Radio Button from the 3 above.
//here i can get to radSelected.Value
如果要为控件分配任意值,请使用其Tag属性:
radColor1.Tag = 5;
但是你为什么不能简单地使用这个Checked属性呢?
if (radColor1.Checked)
    //do something
if (radColor2.Checked)
    //do something else
//...however many more you need to check
编辑:迭代...
foreach (Control c in this.Controls)
{
    if (c is RadioButton)
    {
        if (((RadioButton)c).Checked)
        //do something
    }
}
| 归档时间: | 
 | 
| 查看次数: | 18564 次 | 
| 最近记录: |