articles

Home / DeveloperSection / Articles / Checkbox Control in C#.Net

Checkbox Control in C#.Net

Checkbox Control in C#.Net

Pushpendra Singh 18771 24-Jan-2011

Checkbox Control in C#.Net

The Checkbox control is used to display a checkbox. The Checkbox control gives us an option to select, say, yes/no or true/false. A checkbox is clicked to select and clicked again to deselect some options. When a checkbox is selected, a check (a tick mark) appears indicating a selection.

How to use CheckBox Control

Drag and drop three checkboxes from the toolbox on window form.

Checkbox Control in C#.Net

Code:

using System;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication1
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            //display checkbox value when checkbox1 is checked
            label1.Text = "Selected Language is " + checkBox1.Text;
        }
 
        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            //display checkbox value when checkbox2 is checked
            label1.Text = "Selected Language is " + checkBox2.Text;
        }
        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            //display checkbox value when checkbox3 is checked
            label1.Text = "Selected Language is " + checkBox3.Text;
        }
    }
}

Run the Project

Checkbox Control in C#.Net

When you select the given option then the CheckedChanged event will fire and the selected value will show in Label.

Checkbox Control in C#.Net

CheckBox Control Properties

CheckState:     The default value is Unchecked. Set it to True if you want a check to appear when form executed.

private void Form4_Load(object sender, EventArgs e)
{
     'check will appear at run time
     checkBox1.CheckState = CheckState.Checked;
}     

Output 

Checkbox Control in C#.Net

When the application runs then checkBox1 check state is checked.

Appearance: Default value is Normal. Set the value to Button if you want the CheckBox to be displayed as a Button.

private void Form4_Load(object sender, EventArgs e)
{
        //'set checkbox appearance as button
        checkBox1.Appearance = Appearance.Button;
        checkBox2.Appearance = Appearance.Button;
        checkBox3.Appearance = Appearance.Button;
}

Output

Checkbox Control in C#.Net

Now checkbox will show a button.

BackColor:    You can change the BackColor of CheckBox trough BackColor properties.

private void Form4_Load(object sender, EventArgs e)
{      // change the backcolor of checkbox
       checkBox1.BackColor = System.Drawing.Color.CadetBlue;
       checkBox2.BackColor = System.Drawing.Color.CadetBlue;
       checkBox3.BackColor = System.Drawing.Color.CadetBlue;
}

Output

Checkbox Control in C#.Net

At run, time checkbox backcolor will be changed.


c# c# 
Updated 12-Mar-2020

Leave Comment

Comments

Liked By