Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将不同面板中的单选按钮分组为单个组?_C#_Winforms_Radio Button - Fatal编程技术网

C# 如何将不同面板中的单选按钮分组为单个组?

C# 如何将不同面板中的单选按钮分组为单个组?,c#,winforms,radio-button,C#,Winforms,Radio Button,我必须将RadioButton以单个组的形式分组在不同的面板中。例如,我在panel1上有一个名为radBtn1的radBtn2单选按钮,在panel2上有radBtn2。现在,我想将所有这些单选按钮分组为单个组,以便一次只选中一个RadioButton。我们可以尝试手动取消选中RadioButtons。让我们为所有感兴趣的单选按钮分配单选按钮\u CheckedChanged事件处理程序: private void RadioButtons_CheckedChanged(object send

我必须将
RadioButton
单个组的形式分组在不同的
面板中。例如,我在
panel1
上有一个名为
radBtn1
radBtn2
单选按钮,在
panel2
上有
radBtn2
。现在,我想将所有这些单选按钮分组为单个组,以便一次只选中一个
RadioButton

我们可以尝试手动取消选中
RadioButton
s。让我们为所有感兴趣的
单选按钮分配
单选按钮\u CheckedChanged
事件处理程序:

private void RadioButtons_CheckedChanged(object sender, EventArgs e) {
  // If we have a RadioButton Checked 
  if (sender is RadioButton current && current.Checked) {
    // We should remove Check from the rest RadioButtons:
    var rest = new Control[] { panel1, panel2} 
      .SelectMany(ctrl => ctrl             // all radiobuttons on panel1, panel2
         .Controls
         .OfType<RadioButton>())
      .Where(button => button != current); // don't touch current it should stay checked

    foreach (var button in rest)
      button.Checked = false;
  }
}
private void单选按钮\u CheckedChanged(对象发送方,事件参数e){
//如果我们检查了无线电按钮
如果(发送方为当前单选按钮&¤t.Checked){
//我们应该删除其余单选按钮中的复选框:
var rest=新控件[]{panel1,panel2}
.SelectMany(ctrl=>ctrl//panel1、panel2上的所有单选按钮
.控制
.OfType())
.Where(button=>button!=current);//不要触摸current它应该保持选中状态
foreach(静止状态下的var按钮)
按钮。选中=错误;
}
}

我认为这可能表明UI\UX设计不佳。但是,没有自动机制可以让单选按钮以这种方式相互交互。当每个单选按钮触发
CheckedChanged
事件时,您可能需要编写一些代码来手动取消选中其他单选按钮