当C#中的AutoCheck属性设置为false时,手动选中和取消选中RadioButton的正确方法是什么
本文关键字:取消 RadioButton 是什么 方法 属性 AutoCheck 中的 设置 false | 更新日期: 2023-09-27 18:28:58
我有一组单选按钮,其中两个需要在允许值更改之前用确认对话框提示用户。为此,我处理单击事件,并将每个单选按钮的"自动检查"属性设置为false。但是,当我在单击的单选按钮上将check属性设置为true时,以前选择的单选按钮将不再被取消选中。
现在我只是循环浏览这个面板上的控件,并确保没有选中其他单选按钮,但有更有效的方法吗?
您可以使用一些variable
来存储上次检查的单选按钮:
//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;
}
如果您想允许用户uncheck
无线电(这是一种非常奇怪的行为),只需删除上面代码中else
子句之前的//
即可。