开发者

Select and unselect checkboxes

i have a list of Checkbox's .

checkbox1,checkbox2....

i want to uncheck the chec开发者_如何学运维kbox if its is checked and vice versa.Is there any way to do this.


foreach (CheckBox cb in cbList) {
  cb.Checked = !cb.Checked;
}


if (CheckBox1.Checked == true)
    {
    CheckBox1.Checked = false
    }
    else
    {
    CheckBox1.Checked = true
    }


I don't know if you're working in ASP.NET, WPF, WinForms, ...

But it's as easy as check if checkbox is checked, then uncheck, and viceversa.

Or you can implement an extension method like:

public static class CheckboxExtensions
{
     public static void ToggleChecked(this CheckBox some) 
     {
           if(some != null) 
           {
                  if(some.Checked)
                  {
                        some.Checked = false;
                  }
                  else
                  {
                        some.Checked = true;
                  }
           }
    }
}

... And your actual code will look like this:

chkSome.ToggleChecked();


Try

checkBox1.Checked = !checkBox1.Checked;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜