formcheckbox.cs

来自「csharp课本的源代码」· CS 代码 · 共 103 行

CS
103
字号
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CheckBoxExample
{
    public partial class FormCheckBox : Form
    {
        private void SetCheckState()
        {
            if (checkBoxColor.Checked && checkBoxFont.Checked)
            {
                checkBoxSelectAll.CheckState = CheckState.Checked;
                checkBoxSelectAll.ThreeState = false;
            }
            else if (!checkBoxColor.Checked && !checkBoxFont.Checked)
            {
                checkBoxSelectAll.CheckState = CheckState.Unchecked;
                checkBoxSelectAll.ThreeState = false;
            }
            else
            {
                checkBoxSelectAll.CheckState = CheckState.Indeterminate;
            }
        }

        public FormCheckBox()
        {
            InitializeComponent();
        }

        private void checkBoxColor_CheckedChanged(object sender, EventArgs e)
        {

            if (checkBoxColor.Checked)
            {
                this.BackColor = Color.LightBlue;
            }
            else
            {
                this.BackColor = Color.LightGreen;
            }

        }

        private void checkBoxFont_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxFont.Checked)
            {
                this.Font = new Font(this.Font.FontFamily.Name, 12, this.Font.Style);
            }
            else
            {
                this.Font = new Font(this.Font.FontFamily.Name, 8, this.Font.Style);
            }

        }

        private void checkBoxSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxSelectAll.Checked == false)
            {
                checkBoxColor.Checked = false;
                checkBoxFont.Checked = false;
            }

        }

        private void checkBoxSelectAll_Click(object sender, EventArgs e)
        {
            checkBoxSelectAll.ThreeState = false;
            if (checkBoxSelectAll.Checked)
            {
                checkBoxColor.Checked = true;
                checkBoxFont.Checked = true;
            }
            else if (checkBoxSelectAll.Checked = false)
            {
                checkBoxColor.Checked = false;
                checkBoxFont.Checked = false;
            }

        }

        private void checkBoxColor_Click(object sender, EventArgs e)
        {
            checkBoxSelectAll.ThreeState = true;
            SetCheckState();

        }

        private void checkBoxFont_Click(object sender, EventArgs e)
        {
            checkBoxSelectAll.ThreeState = true;
            SetCheckState();

        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?