gpt4 book ai didi

c# - 在单独的面板中配对单选按钮

转载 作者:太空宇宙 更新时间:2023-11-03 10:21:48 25 4
gpt4 key购买 nike

我有一个 Windows 窗体应用程序,它在窗体中显示了一些用户控件。每个用户控件都有一个单选按钮。是否可以手动配对单选按钮以便同时只能选择其中一个?我的另一个选择是在单击单选按钮时检查每个单选按钮,以便取消选中所有其他单选按钮。

最佳答案

        RadioButton rb1 = new RadioButton { Text = "RB1" };
RadioButton rb2 = new RadioButton { Text = "RB2" };
RadioButtonGroup rgb = new RadioButtonGroup(rb1, rb2);

foreach (RadioButton rb in new [] { rb1, rb2 }) {
Form f = new Form { Text = rb.Text };
f.Controls.Add(rb);
f.Show();
rb.CheckedChanged += delegate {
MessageBox.Show(rb.Text + ": " + rb.Checked);
};
}



private class RadioButtonGroup {
RadioButton[] radioButtons = null;
public RadioButtonGroup(params RadioButton[] radioButtons) {
this.radioButtons = radioButtons;
foreach (var rb in radioButtons) {
rb.AutoCheck = false;
rb.Click += rb_Click;
}
}

void rb_Click(object sender, EventArgs e) {
foreach (RadioButton rb in radioButtons)
rb.Checked = (rb == sender);
}
}

关于c# - 在单独的面板中配对单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33237459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com