c# - 当 AutoCheck 属性在 C# 中设置为 false 时,手动选中和取消选中 RadioButton 的正确方法是什么?

标签 c# .net winforms .net-4.0 radio-button

我有一组单选按钮,其中两个需要在允许更改值之前通过确认对话框提示用户。为此,我处理了单击事件并将每个单选按钮的 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/

相关文章:

C# 浏览器缩放

c# - 为什么 C# 没有私有(private)包?

c# - 如何列出 MVC 用户及其角色?

c# - 如何处理阻塞的剪贴板和其他异常情况

c# - 如何使用 TreeView 连接 Silverlight 和 MVVM 中的 View ?

c# - 开头没有选定行的 DataGridView

c# - 使用 LINQ 在 C# 中使用 MasterMind 评分算法

c# - 在 Effort 中取消强制外键引用

c# - 将简单字符串从一种形式传递到另一种形式

c# - ConfigurationManager 连接字符串在 UserControl 中为空