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

Checkbox Control in C#.Net


C# C# 
Ratings:
0 Comment(s)
 11208  View(s)
Rate this:

Checkbox Control in C#.Net

The Checkbox control is used to display a check box. 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 option. When a checkbox is selected, a check (a tick mark) appears indicating a selection.

How to use CheckBox Control

Drag and drop three checkbox from 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 CheckedChanged event will fire and selected value will show in Label.

Checkbox Control in C#.Net

CheckBox Control Properties

CheckState:     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 application run then checkBox1 checkstate 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 as 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.

 

 

 

 

 


Don't want to miss updates? Please click the below button!

Follow MindStick