C #, RadioButton select and deselect (a kind of no way checked and unchecked switch)

In doing today RadioButton controls in Winform encountered a pit, although very simple, but I wasted ten minutes, I feel the need to record it encourage each other.

Problem Description: (RadioButton control named rbTime)

Originally thought to determine what is selected, and not allowed to select the check, and vice versa, the result has been not selected

  private void rbTime_Click(object sender, EventArgs e)
  {
      if (rbTime.Checked == true)
      {
         rbTime.Checked = false;
      }
      else if (rbTime.Checked == false)
      {
         rbTime.Checked = true;
      }
 }

Solution:

Declare a local variable, which modify the attribute values ​​of local variables

 //局部变量
private bool rbcheck = true;

if (rbcheck)
   {
        rbTime.Checked = false;
        rbcheck = false;
   }
else
  {
        rbTime.Checked = true;
        rbcheck = true;
   }

 

 

Guess you like

Origin www.cnblogs.com/yuanshuo/p/11549628.html