当前位置:网站首页>Basic use of check boxes and implementation of select all and invert selection functions

Basic use of check boxes and implementation of select all and invert selection functions

2022-06-23 08:15:00 Scarlett2025

            Checked attribute , Mark the current checkbox status ( When selected, it is True otherwise False)
            ThreeState There are three selected states
             according to CheckState To judge (Checked[ Choose ] , Unchecked[ No choice ] , Indeterminate[ Tree structure , A state of being , Indicates that there are one or more children under the current tree structure ( Not all children ) To be selected ])
             CheckedChanged event : Indicates whether the current control is selected

The layout of the page is as follows :

double-click 【 select all 】 Button , The code is as follows :

private void AllSelected_Click(object sender, EventArgs e)
{
    for (int i = 0; i < this.Controls.Count; i++)
    {
         CheckBox cb = this.Controls[i] as CheckBox; // equivalent 
         // resolvent 1 , Judge cb Is it null.
         if (cb != null)
         {
             cb.Checked = true;
          }
      }
}

double-click 【 Deselect all 】 Button , The code is as follows :

private void CancelAll_Click(object sender, EventArgs e)
{
     for (int i = 0; i < this.Controls.Count; i++)
     {
         CheckBox cb = this.Controls[i] as CheckBox;
         // resolvent 2, Determine the current control's type
         if (this.Controls[i].GetType() == typeof(CheckBox))               
         {
             cb.Checked = false;
          }
      }
}

原网站

版权声明
本文为[Scarlett2025]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230752491873.html