Open In App

How to set the state of the CheckBox in C#?

The CheckBox control is the part of the windows form that 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. In CheckBox, you are allowed to get or set the state of the CheckBoxes using the CheckState property of the CheckBox. In CheckBox, there are three different types of states are available: 

CheckState For Normal Appearance For Button Appearance
Checked The CheckBox shows a checkmark. The control looks sunken.
UnChecked The CheckBox is vacant. The control looks raised.
Indeterminate The CheckBox shows a checkmark and is shaded. The control looks flat.

The default value of the CheckState property is Unchecked. In Windows form, you can set this property in two different ways:



1. Design-Time: It is the simplest way to set the CheckState property of a CheckBox using the following steps:  



Output: 

2. Run-Time: It is a little bit trickier than the above method. In this method, you can set the CheckState property of a CheckBox programmatically using the following syntax:

public System.Windows.Forms.CheckState CheckState { get; set; }

Here, the CheckState is used to represent the CheckState enumeration values. It also throws an InvalidEnumArgumentException if the value assigned to the CheckState property does not belong to the CheckState enumeration values. Following steps are used to set the CheckState property of the CheckBox: 

// Creating checkbox
CheckBox Mycheckbox = new CheckBox();
// Set the CheckState property of the CheckBox
Mycheckbox1.CheckState = CheckState.Indeterminate;
// Add this checkbox to form
this.Controls.Add(Mycheckbox);

Example:




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 Gender:";
        l.Location = new Point(233, 111);
 
        // Adding label 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 = "Male";
        Mycheckbox.CheckState = CheckState.Unchecked;
 
        // Adding checkbox to form
        this.Controls.Add(Mycheckbox);
 
        // Creating and setting the properties of CheckBox
        CheckBox Mycheckbox1 = new CheckBox();
        Mycheckbox1.Height = 50;
        Mycheckbox1.Width = 100;
        Mycheckbox1.Location = new Point(230, 174);
        Mycheckbox1.Text = "Female";
        Mycheckbox1.CheckState = CheckState.Indeterminate;
 
        // Adding checkbox to form
        this.Controls.Add(Mycheckbox1);
    }
}
}

Output: 

 


Article Tags :
C#