我有一组单选按钮,其中两个需要在允许更改值之前通过确认对话框提示用户。为此,我处理了单击事件并将每个单选按钮的 AutoCheck 属性设置为 false。但是,当我在单击的 RadioButton 上将检查属性设置为 true 时,以前选择的 RadioButton 不再处于未选中状态。
现在我只是循环浏览此面板上的控件并确保没有选中任何其他 RadioButtons,但是否有更有效的方法来执行此操作?
最佳答案
您可以使用一些变量
来存储最后检查的单选按钮:
//first, you have to set the lastChecked = radioButton1 (or another of your radioButtons)
RadioButton lastChecked;
//Click event handler used for all the radioButtons
private void RadiosClick(object sender, EventArgs e)
{
RadioButton radio = sender as RadioButton;
if (radio != lastChecked){
radio.Checked = true;
lastChecked.Checked = false;
lastChecked = radio;
}
//else radio.Checked = !radio.Checked;
}
如果你想让用户取消选中
radio (一个非常奇怪的行为),只需删除else
子句之前的//
在上面的代码中。
关于c# - 当 AutoCheck 属性在 C# 中设置为 false 时,手动选中和取消选中 RadioButton 的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18406029/