【窗体控件】:RadioButton和CheckBox控件

1、命名空间与继承

命名空间:System.Windows.Forms

继承:Object→MarshalByRefObject→Component→Control→ButtonBase→RadioButton[CheckBox]

2、常用属性

Checked:是否被选中

注意,RadioButton和CheckBox一般和组件搭配使用,一般放置在GroupBox或者Panel之中

3、常用事件

private void radioButton1_CheckedChanged(object sender, EventArgs e)

4、官方示例

无官方例子

		//RadioButton事件示例
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            if(rb != null)
            {
                if (rb.Checked)
                {
                    this.label3.Text = "radioButton选择结果:"+ rb.Text;
                }
            }
        }
	//CheckBox事件示例
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            string s = "cb选择结果:";
            foreach (var item in this.groupBox3.Controls)
            {
                CheckBox cb = item as CheckBox;
                if (cb != null)
                {
                    if(cb.Checked)s+= " " +cb.Text;
                }
            }
            this.label4.Text = s;
        }

两种状态的CheckBox

在这里插入图片描述

		private void Form1_Load(object sender, EventArgs e)
		{
            //两种状态Checkbox作为按钮使用
            //设置未选中状态
            this.checkBox5.Checked = false;
            this.checkBox5.Text = "已经停止";
            this.checkBox5.ForeColor = Color.Snow;
            this.checkBox5.BackColor = Color.Red;
            this.checkBox5.Appearance = Appearance.Button;//Appearance外观选择Button模式
            this.checkBox5.FlatStyle=FlatStyle.Flat;//FlatStyle选择Flat
            this.checkBox5.FlatAppearance.BorderSize = 0;//必须先设置FlatStyle,再设置FlatAppearence.BorderSize为0才有效
		}
        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            if(cb != null)
            {
                if(cb.Checked)
                {
                    this.checkBox5.Text = "正在摸鱼";
                    this.checkBox5.ForeColor = Color.NavajoWhite;
                    this.checkBox5.BackColor = Color.Green;
                }
                else
                {
                    this.checkBox5.Text = "已经停止";
                    this.checkBox5.ForeColor = Color.Snow;
                    this.checkBox5.BackColor = Color.Red;
                }
            }
        }

二选一的RadioButton

在这里插入图片描述

private void Form1_Load(object sender, EventArgs e)
{
   //两种状态的RadioButton作为按钮使用
   this.label7.Text = "二选一";
   //设置未选中状态
   this.radioButton3.Checked = false;
   this.radioButton3.Text = "未启用:发呆功能";
   this.radioButton3.ForeColor = Color.Blue;
   this.radioButton3.BackColor = Color.DarkGray;
   this.radioButton3.Appearance = Appearance.Button;//Appearance外观选择Button模式
   this.radioButton3.FlatStyle = FlatStyle.Flat;//FlatStyle选择Flat
   this.radioButton3.FlatAppearance.BorderSize = 0;//必须先设置FlatStyle,再设置FlatAppearence.BorderSize为0才有效

   this.radioButton4.Checked = true;
   this.radioButton4.Text = "已启用:吹牛功能";
   this.radioButton4.ForeColor = Color.Black;
   this.radioButton4.BackColor = Color.Red;
   this.radioButton4.Appearance = Appearance.Button;//Appearance外观选择Button模式
   this.radioButton4.FlatStyle = FlatStyle.Flat;//FlatStyle选择Flat
   this.radioButton4.FlatAppearance.BorderSize = 0;//必须先设置FlatStyle,再设置FlatAppearence.BorderSize为0才有效
}

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton cb = sender as RadioButton;
            
            if (cb != null)
            {
                if (cb.Checked)
                {
                    //更改状态
                    //cb.Checked = false;

                    this.radioButton3.Text = "已启用:发呆功能";
                    this.radioButton3.ForeColor = Color.Black;
                    this.radioButton3.BackColor = Color.Red;
                    
                }
                else
                {
                    this.radioButton3.Text = "未启用:发呆功能";
                    this.radioButton3.ForeColor = Color.Blue;
                    this.radioButton3.BackColor = Color.DarkGray;
                }
            }
        }
        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton cb = sender as RadioButton;

            if (cb != null)
            {
                if (cb.Checked)
                {
                    //更改状态
                    //cb.Checked = false;

                    this.radioButton4.Text = "已启用:吹牛功能";
                    this.radioButton4.ForeColor = Color.Black;
                    this.radioButton4.BackColor = Color.Red;
                }
                else
                {
                    this.radioButton4.Text = "未启用:吹牛功能";
                    this.radioButton4.ForeColor = Color.Blue;
                    this.radioButton4.BackColor = Color.DarkGray;
                }
            }
        }

5、其他

  • CheckBox的ThreeState->
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值