如何让checkBox控件全选、反选、取消包含在groupBox内其它checkBox控件

解决方案 »

  1.   


    全选、取消
                foreach (Control control in this.groupBox.Controls)
                {
                    if (control is CheckBox)
                    {
                        (control as CheckBox).Checked= checkBox.Checked;
                    }
                }反选
                foreach (Control control in this.groupBox.Controls)
                {
                    if (control is CheckBox)
                    {
                        (control as CheckBox).Checked= !(control as CheckBox).Checked;
                    }
                }
      

  2.   

    如果checkbox包含在groupbox而不是直接放在窗体里,在遍历的时候就需要嵌套遍历
    foreach(control a in this.controls)
    {
    if(a.gettype().name=="checkbox")
    {
      a.checked="true";//将直接放在窗体上的所有checkbox全选
    }
    if(a.gettype().name=="groupbox")
    {
      foreach(control b in a.controls)
      {
         if(b.gettype().name=="checkbox")
         {
           b.checked="true";//将放在groupbox中的所有checkbox全选
         }  }
    }
    }若checkbox放在了groupbox里的groupbox里,则再加foreach嵌套即可
      

  3.   

    谢谢各位份的帮助!我的问题解决了!COOL!