Learn how to use CheckBoxes and handle various state events
This tutorial will help you learn the following concepts:
- How to add numerous CheclBoxes in a
Form
programmatically. - How to autocheck or explicitly Check or Uncheck a CheckBox in C#.
- How to Handle click event of the CheckBox:
- How to handle the
CheckedChanged
event of a CheckBox in C# - How to manually Check or Uncheck a CheckBox
- How to handle the
CheckStateChanged
event of a Button - How to set the
CheckState
value of a CheckBox - How to change the Appearance of a CheckBox in C#.
Example 1: Simple CheckBox Example
Learn CheckBox usage and how to handle Click and Checked events.
This example will comprise the following files:
CheckBox.cs
Step 1: Create Project
- The first step is to create a C# Project.
- Go to
FILE-->New-->Project
to create a new project
Step 2: Write Code
Write Code as follows:
*(a). CheckBox.cs
Create a file named CheckBox.cs
Add the following imports
using System;
using System.Windows.Forms;
Create our namespace:
namespace Examples {
Extend the Form
class:
class Form1 : Form {
Create our Main()
method
public static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form1());
}
Define our constructor
:
public Form1() {
Set the text
property of our Form and then add several checkbox controls onto it:
Text = "CheckBox example";
Controls.AddRange(new Control[] {checkBox1, checkBox2, checkBox3, checkBox4, checkBox5});
Set the checkBox's AutoCheck
property to false:
checkBox1.AutoCheck = false;
Handle click event of the CheckBox:
checkBox1.Click += delegate {
//checkBox1.Checked = !checkBox1.Checked;
//checkBox1.Text = string.Format("{0}", checkBox1.CheckState);
};
Here is how you handle the CheckedChanged
event of CheckBox:
checkBox2.CheckedChanged += delegate (object sender, EventArgs e) {
checkBox2.Text = string.Format("{0}", checkBox2.CheckState);
};
You can manually set Checked
value as shown below:
checkBox2.Checked = true;
You can also react to CheckStateChanged
events as shown below:
checkBox3.CheckStateChanged += delegate (object sender, EventArgs e) {
checkBox3.Text = string.Format("{0}", checkBox3.CheckState);
};
You can also set the CheckState
property explicitly:
checkBox3.CheckState = CheckState.Indeterminate;
If you want the CheckBox to allow 3 states rather than 2 then use the ThreeState
property:
checkBox3.ThreeState = true;
You can also change the appearance of a CheckBox using the Appearance
property. You can assign it a Normal
or a Button
property:
checkBox4.Appearance = Appearance.Button;
Here is how we instantiate our CheckBoxes:
private CheckBox checkBox1 = new CheckBox();
private CheckBox checkBox2 = new CheckBox();
private CheckBox checkBox3 = new CheckBox();
private CheckBox checkBox4 = new CheckBox();
private CheckBox checkBox5 = new CheckBox();
Here is the full code
using System;
using System.Windows.Forms;
//end
namespace Examples {
//end
class Form1 : Form {
//end
public static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form1());
}
//end
public Form1() {
//end
Text = "CheckBox example";
Controls.AddRange(new Control[] {checkBox1, checkBox2, checkBox3, checkBox4, checkBox5});
//end
checkBox1.AutoCheck = false;
//end
checkBox1.Click += delegate {
//checkBox1.Checked = !checkBox1.Checked;
//checkBox1.Text = string.Format("{0}", checkBox1.CheckState);
};
//end
checkBox1.Location = new System.Drawing.Point(30, 30);
checkBox1.Text = string.Format("{0}", checkBox1.CheckState);
checkBox2.CheckedChanged += delegate (object sender, EventArgs e) {
checkBox2.Text = string.Format("{0}", checkBox2.CheckState);
};
//end
checkBox2.Checked = true;
//end
checkBox2.Location = new System.Drawing.Point(30, 60);
checkBox2.Checked = true;
checkBox3.CheckStateChanged += delegate (object sender, EventArgs e) {
checkBox3.Text = string.Format("{0}", checkBox3.CheckState);
};
//end
checkBox3.CheckState = CheckState.Indeterminate;
//end
checkBox3.Location = new System.Drawing.Point(30, 90);
checkBox3.Text = string.Format("{0}", checkBox3.CheckState);
checkBox3.ThreeState = true;
//end
checkBox4.AutoCheck = false;
checkBox4.Appearance = Appearance.Button;
//end
checkBox4.Click += delegate {
//checkBox4.Checked = !checkBox4.Checked;
//checkBox4.Text = string.Format("{0}", checkBox4.CheckState);
};
checkBox4.Location = new System.Drawing.Point(30, 120);
checkBox4.Text = string.Format("{0}", checkBox4.CheckState);
checkBox5.Appearance = Appearance.Button;
checkBox5.CheckStateChanged += delegate (object sender, EventArgs e) {
checkBox5.Text = string.Format("{0}", checkBox5.CheckState);
};
checkBox5.Checked = true;
checkBox5.Location = new System.Drawing.Point(30, 150);
}
private CheckBox checkBox1 = new CheckBox();
private CheckBox checkBox2 = new CheckBox();
private CheckBox checkBox3 = new CheckBox();
private CheckBox checkBox4 = new CheckBox();
private CheckBox checkBox5 = new CheckBox();
//end
};
}
Run
Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj
project, build and run.
Reference
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |
Example 2: GroupBox And CheckBox
This example will comprise the following files:
GroupBoxAndCheckBox.cs
Step 1: Create Project
- The first step is to create a C# Project.
- Go to
FILE-->New-->Project
to create a new project.
Step 2: Write Code
Write Code as follows:
*(a). GroupBoxAndCheckBox.cs
Create a file named GroupBoxAndCheckBox.cs
Here is the full code
using System;
using System.Windows.Forms;
namespace Examples {
class Form1 : Form {
// The main entry point for the application.
public static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form1());
}
Form1() {
this.StartPosition = FormStartPosition.Manual;
this.Location = new System.Drawing.Point(400, 200);
this.ClientSize = new System.Drawing.Size(300, 160);
this.Text = "GroupBox and CheckBox example";
this.group1.Parent = this;
this.group1.Text = "Group 1";
this.group1.Bounds = new System.Drawing.Rectangle(10, 10, 135, 140);
this.check1.Parent = this.group1;
this.check1.Location = new System.Drawing.Point(20, 20);
this.check1.Text = "check 1";
this.check2.Parent = this.group1;
this.check2.Location = new System.Drawing.Point(20, 50);
this.check2.Text = "check 2";
this.check2.Checked = true;
this.check3.Parent = this.group1;
this.check3.Location = new System.Drawing.Point(20, 80);
this.check3.Text = "check 3";
this.group2.Parent = this;
this.group2.Text = "Group 2";
this.group2.Bounds = new System.Drawing.Rectangle(155, 10, 135, 140);
this.check4.Parent = this.group2;
this.check4.Location = new System.Drawing.Point(20, 20);
this.check4.Text = "check 4";
this.check5.Parent = this.group2;
this.check5.Location = new System.Drawing.Point(20, 50);
this.check5.Text = "check 5";
this.check6.Parent = this.group2;
this.check6.Location = new System.Drawing.Point(20, 80);
this.check6.Text = "check 6";
this.check6.Checked = true;
}
private GroupBox group1 = new GroupBox();
private GroupBox group2 = new GroupBox();
private CheckBox check1 = new CheckBox();
private CheckBox check2 = new CheckBox();
private CheckBox check3 = new CheckBox();
private CheckBox check4 = new CheckBox();
private CheckBox check5 = new CheckBox();
private CheckBox check6 = new CheckBox();
}
}
Run
Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj
project, build and run.
Reference
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |