CheckBox in C#

The CheckBox control is the part of windows form which is used to take input from the user. Or in other words, CheckBox control allows us to select single or multiple elements from the given list or it can provide us options like yes or no, true or false, etc. It can be displayed as an image or text or both. The CheckBox is a class and defined under System.Windows.Forms namespace. In Windows form, you can create CheckBox in two different ways:

1. Design-Time: It is the simplest way to create a CheckBox using the following steps:

  • Step 1: Create a windows form as shown in the below image:
    Visual Studio -> File -> New -> Project -> WindowsFormApp
  • Step 2: Drag the CheckBox control from the ToolBox and drop it on the windows form. You can place CheckBox anywhere on the windows form according to your need.
  • Step 3: After drag and drop you will go to the properties of the CheckBox control to modify the CheckBox design according to your requirement.



    Output:

2. Run-Time: It is a little bit trickier than the above method. In this method, you can create your own checkbox programmatically using the CheckBox class.

  • Step 1: Create a checkbox using the CheckBox() constructor provided by the CheckBox class.
    // Creating checkbox
    CheckBox Mycheckbox = new CheckBox();
    
  • Step 2: After creating CheckBox, set the properties of the CheckBox provided by the CheckBox class.
    // 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);
    
  • Step 3 : And last add this checkbox control to form using Add() method.
    // Add this checkbox to form
    this.Controls.Add(Mycheckbox);
    

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    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);
        }
    }
    }

    chevron_right

    
    

    Output:

Important Properties of CheckBox

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.

Important Events on 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.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.