WinForm控件之【CheckBox】

基本介绍

复选框顾名思义常用作选择用途,常见的便是多选项的使用;

常设置属性、事件

Checked:指示组件是否处于选中状态,true为选中处于勾选状态,false为未选中空白显示;

Enabled:指示是否启用该控件,true为启用状态用户可直接勾选更改状态,false为禁用状态呈现浅灰状态用户无法直接勾选;

Name:指示代码中用来标识该对象的名称;

Text:与控件关联的文本,显示给用户看的内容说明;

CheckedChanged 事件:每当组件Checked状态更改时发生,一般想用复选框组件触发操作其他事务任务时使用该事件;

事例举例

        //全选与反选共用事件
        private void ck_All_CheckedChanged(object sender, EventArgs e)
        {
            if (sender is CheckBox)
            {
                CheckBox cb = sender as CheckBox;
                if (cb.Checked)
                {
                    if (cb.Text.Equals("全选"))
                    {
                        if (ck_ReverseCheck.Checked)
                        {
                            this.ck_ReverseCheck.Checked = false;
                        }

                        foreach (Control con in panel1.Controls)
                        {
                            if (con is CheckBox)
                            {
                                ((CheckBox)con).Checked = true;
                            }
                        }
                    }
                    else
                    {
                        if (ck_AllCheck.Checked)
                        {
                            this.ck_AllCheck.CheckedChanged -= new System.EventHandler(this.ck_All_CheckedChanged);
                            this.ck_AllCheck.Checked = false;
                            this.ck_AllCheck.CheckedChanged += new System.EventHandler(this.ck_All_CheckedChanged);
                        }

                        foreach (Control con in panel1.Controls)
                        {
                            if (con is CheckBox)
                            {
                                ((CheckBox)con).Checked = !((CheckBox)con).Checked;
                            }
                        }
                    }
                }
                else
                {
                    if (cb.Text.Equals("全选"))
                    {
                        foreach (Control con in panel1.Controls)
                        {
                            if (con is CheckBox)
                            {
                                ((CheckBox)con).Checked = false;
                            }
                        }
                    }
                }
            }
        }

 复选框的使用根据需求的不同也是可以拥有多元化的设置,

原文地址:https://www.cnblogs.com/ljhandsomeblog/p/11126277.html