📜  C#中的复选框

📅  最后修改于: 2021-05-30 00:56:10             🧑  作者: Mango

CheckBox控件是Windows窗体的一部分,用于接收用户输入。换句话说,CheckBox控件允许我们从给定列表中选择单个或多个元素,或者可以为我们提供诸如yes或no,true或false等选项。它可以显示为图像或文本,或两者都显示。 CheckBox是一个类,在System.Windows.Forms命名空间下定义。在Windows窗体中,可以用两种不同的方式创建CheckBox:

1.设计时:这是使用以下步骤创建CheckBox的最简单方法:

  • 第1步:创建一个Windows窗体,如下图所示:
    Visual Studio->文件->新建->项目-> WindowsFormApp
  • 步骤2:从工具箱中拖动CheckBox控件,并将其放在Windows窗体上。您可以根据需要将CheckBox放置在Windows窗体上的任何位置。
  • 步骤3:拖放之后,您将转到CheckBox控件的属性,以根据需要修改CheckBox设计。

    输出:

2.运行时:比上述方法有些棘手。在这种方法中,您可以使用CheckBox类以编程方式创建自己的复选框。

  • 步骤1:使用CheckBox类提供的CheckBox()构造函数创建一个复选框。
    // Creating checkbox
    CheckBox Mycheckbox = new CheckBox();
    
  • 步骤2:创建CheckBox之后,设置CheckBox类提供的CheckBox属性。
    // Set height of the checkbox
     Mycheckbox.Height = 50;
    
    // Set width of the checkbox
    Mycheckbox.Width = 100;
    
    // Set location of the checkbox
    Mycheckbox.Location = new Point(229, 136);
    
    // Set text in the checkbox
    Mycheckbox.Text = "Married";
    
    // Set font of the checkbox
    Mycheckbox.Font = new Font("Bradley Hand ITC", 12);
    
  • 步骤3:最后,使用Add()方法将此复选框控件添加到表单中。
    // Add this checkbox to form
    this.Controls.Add(Mycheckbox);
    

    例子:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
      
    namespace WindowsFormsApp5 {
      
    public partial class Form1 : Form {
      
        public Form1()
        {
            InitializeComponent();
        }
      
        private void Form1_Load(object sender, EventArgs e)
        {
            // Creating and setting the properties of label
            Label l = new Label();
            l.Text = "Select Status:";
            l.AutoSize = true;
            l.Location = new Point(233, 111);
            l.Font = new Font("Bradley Hand ITC", 12);
            // Adding lable to form
            this.Controls.Add(l);
      
            // Creating and setting the properties of CheckBox
            CheckBox Mycheckbox = new CheckBox();
            Mycheckbox.Height = 50;
            Mycheckbox.Width = 100;
            Mycheckbox.Location = new Point(229, 136);
            Mycheckbox.Text = "Married";
            Mycheckbox.Font = new Font("Bradley Hand ITC", 12);
      
            // Adding checkbox to form
            this.Controls.Add(Mycheckbox);
      
            // Creating and setting the properties of CheckBox
            CheckBox Mycheckbox1 = new CheckBox();
            Mycheckbox1.Location = new Point(230, 198);
            Mycheckbox1.Text = "UnMarried";
            Mycheckbox1.AutoSize = true;
            Mycheckbox1.Font = new Font("Bradley Hand ITC", 12);
      
            // Adding checkbox to form
            this.Controls.Add(Mycheckbox1);
        }
    }
    }
    

    输出:

复选框的重要属性

Property Description
Appearance This property is used to gets or sets the value that indicates the appearance of a CheckBox control.
AutoCheck This property is used to set a value which shows whether the Checked or CheckState values and the CheckBox appearance are automatically changed when you click the CheckBox.
AutoEllipsis This property is used to get or set a value which determine whether the ellipsis character (…) appears at the right edge of the control, denoting that the control text extends beyond the specified length of the control.
AutoSize This property is used to get or set a value that determines whether the control resizes based on its contents.
BackColor This property is used to get or set the background color of the control.
BackgroundImage This property is used to get or set the background image displayed in the control.
CheckState This property is used to get or set the state of the CheckBox.
CheckAlign This property is used to get or set the horizontal and vertical alignment of the check mark on a CheckBox control.
Checked This property is used to get or set a value which determine whether the CheckBox is in the checked state.
Events This property is used to get the list of event handlers that are attached to this Component.
Font This property is used to get or set the font of the text displayed by the control.
ForeColor This property is used to get or set the foreground color of the control.
Image This property is used to get or set the image that is displayed on a checkbox control.
Location This property is used to get or set the coordinates of the upper-left corner of the CheckBox control relative to the upper-left corner of its form.
Margin This property is used to get or set the space between controls.
Name This property is used to get or set the name of the control.
Padding This property is used to get or set padding within the control.
Text This property is used to get or set the text associated with this control.
TextAlign This property is used to get or set the alignment of the text on the CheckBox control.
Visible This property is used to get or set a value which determine whether the control and all its child controls are displayed.

CheckBox上的重要事件

Event Description
CheckedChanged This event occur when the value of the Checked property changes.
CheckStateChanged This event occur when the value of the CheckState property changes.
Click This event occur when the control is clicked.
DoubleClick This event occur when the user double-clicks the CheckBox control.
Leave This event occur when the input focus leaves the control.
MouseClick This event occur when the control is clicked by the mouse.
MouseDoubleClick This event occur when the user double-clicks the CheckBox control.
MouseHover This event occur when the mouse pointer rests on the control.