c# - 如何将不同面板中存在的单选按钮分组为一个组?
问题描述
我必须将RadioButton
存在于不同 Panel
s 中的 s 分组为一个组。Sa,y 例如,我有一个RadioButton
名为radBtn1
onpanel1
和on 。现在我想将所有这些单选按钮分组为一个组,以便一次只检查一个。radBtn2
radBtn2
panel2
RadioButton
解决方案
我们可以尝试手动RadioButton
取消选中s 。让我们为所有感兴趣的人分配事件处理程序:RadioButtons_CheckedChanged
RadioButtons
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;
}
}
推荐阅读
- laravel - 请求输入在中间件中返回 null
- r - 如何更改 R 中 effect_plot (jtools) 的间隔颜色?
- spring-boot - spring-boot升级后的404
- google-drive-api - Google Drive API 成员限制
- neo4j - Neo4j (cypher-shell) 为现有节点创建关系太慢
- c# - 如何在通过 dotnet CLI 安装 NuGet 包时运行命令
- function - COMSOL 中作为边界条件的输入函数
- monogame - MGCB 编辑器在我启动时崩溃
- python - 有没有什么快速的方法来转换数据框中的所有值 - python
- firebase - Flutter:流不从子集合中检索数据