zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C#控件CheckBox的使用

c# 控件 checkbox 使用
2023-09-27 14:22:14 时间

CheckBox控件使用
今天使用到了CheckBox控件,在这记录一下。

工具:Visual Studio 2017(其他版本也行)

方法/步骤:
1、将CheckBox控件拖到窗口里面,如下图所示:
在这里插入图片描述
2、将该控件的名字改成CBox
在这里插入图片描述
3、该控件的常用方法:
this.CheckBox.Checked = true; //设置该控件状态为勾选上
this.CheckBox.Checked = false; //设置该控件状态为未选中
//或者
this.CheckBox.CheckState = CheckState.Checked; //设置该控件状态为勾选上
this.CheckBox.CheckState = CheckState.Unchecked; //设置该控件状态为未选中
4、如何判断现在该控件的状态
this.CBox.Checked

if(this.CBox.Checked==ture)
{
MessageBox.Show(“Choose”);
}
else
{
MessageBox.Show(“No Choose”);
}
5、双击该控件,就会添加一个事件(当该控件状态改变时)

此时会产生一个该属性相关的函数,通过编辑该函数即可实现相关的功能,如下所示:

   private void CBox_CheckedChanged(object sender, EventArgs e)
    {
        if (this.CBox.Checked == true)
        {
            MessageBox.Show("Choose");
        }
        else
        {
            MessageBox.Show("No Choose");
        }
    }