EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Checkbox in C#

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » C# Tutorial » Checkbox in C#

Checkbox-in-C#

Introduction to Checkbox in C#

CheckBox is a control that allows the user to make single or multiple selections from a list of options. In C#, CheckBox class from System.Windows.Forms namespace is used to work with checkbox control. It is a part of Windows Forms and is used to take input from the user. It can also be used to select from the options such as true/false or yes/no.

A user can click on a checkbox to select the associated item and can click on it again to deselect the item. It can be used along with an image or text or both.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Types of Checkbox

We can create checkbox in two different ways:

  1. Using Form Designer at design time.
  2. Using CheckBox class in code at run time.

We can create checkBox at design time by dragging a checkbox control from the ToolBox and then dropping it on the windows form. Then, we can go to the properties of the checkbox control and can modify it.

To create a checkbox at runtime, we need to use the CheckBox class.

Syntax: 

CheckBox check_box = new CheckBox();

After this, we can set the properties of the checkbox according to our requirements.

//setting location of checkbox
check_box.Location = new Point(300,150);
//setting height and width for checkbox
check_box.Height = 50;
check_box.Width = 50;
//setting text for checkbox
check_box.Text = “Yes”;

Popular Course in this category
C# Training Program (6 Courses, 17 Projects)6 Online Courses | 17 Hands-on Project | 89+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.6 (8,847 ratings)
Course Price

View Course

Related Courses
ASP.NET Training (8 Courses, 19 Projects).NET Training Program (4 Courses, 19 Projects)

At last, we need to add this checkbox to the Windows Form by using:

this.Controls.Add(check_box);

CheckBox Properties

C# provides many properties for the checkbox.

Property Description
AllowDrop It is used to get or set a value that determines whether the checkbox control can accept data that a user drags onto it.
Appearance It is used to get or set a value that determines the appearance of the checkbox control.
AutoCheck It is used to get or set a value that determines whether the values of the Checked or CheckState properties or the appearance of the checkbox are automatically changed when the checkbox is clicked.
AutoSize It is used to get or set a value that determines whether the checkbox control resizes based on its content.
BackColor It is used to get or set the background color of the checkbox control.
BackGroundImage It is used to get or set the background image displayed in the checkbox control.
CanFocus It is used to get a value that determines whether the checkbox control can receive focus.
Checked It is used to get or set a value that determines whether the checkbox control is in the checked state.
CheckState It is used to get or set the state of the checkbox.
DefaultSize It is used to get the default size of the checkbox control.
Enabled It is used to get or set a value that determines whether the checkbox control can respond to user interaction.
Focused It is used to get a value that determines whether the checkbox control has input focus.
Font It is used to get or set the font of text displayed by the checkbox control.
ForeColor It is used to get or set the foreground color of the checkbox control.
Height It is used to get or set the height of the checkbox control.
Image It is used to get or set the image which is displayed on the checkbox control.
Location It is used to get or set the coordinates of the upper left corner of the control relative to the upper left corner of its parent container.
Margin It is used to get or set the space between controls.
Name It is used to get or set the name of the checkbox control.
Size It is used to get or set the height and width of the checkbox control.
Text It is used to get or set the text associated with the checkbox control.
ThreeState It is used to get or set a value that determines whether the checkbox will allow three check states instead of two.
Width It is used to get or set the width of the checkbox control.

CheckBox Events

Let us see some important events for the CheckBox provided by C#:

Event Description
CheckedChanged This event occurs when the value of Checked property changes.
CheckStateChanged This event occurs when the value of CheckState property changes.
Click This event occurs when the checkbox is clicked.
GotFocus This event occurs when the checkbox receives focus.
Leave This event occurs when the input focus leaves the checkbox.
LostFocus This event occurs when the checkbox loses focus.
MouseClick This event occurs when the checkbox is clicked by the mouse.
MouseDoubleClick This event occurs when the checkbox is double-clicked by a mouse.
TextChanged This event occurs when the value of Text property changes.

Implementation of CheckBox in C#

Below is an example of how to implement the checkbox in c #

Example:

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 WindowsFormsApp1
{
public partial class CheckBoxDemo : Form
{
public CheckBoxDemo()
{
InitializeComponent();
}
private void CheckBoxDemo_Load(object sender, EventArgs e)
{
//Creating and setting properties of Label
Label label = new Label();
label.Text = "Select your technical skills";
label.AutoSize = true;
label.Location = new Point(192, 77);
label.Font = new Font("Microsoft Sans Serif", 11);
//Adding label to form
this.Controls.Add(label);
//Creating and setting properties of CheckBox
CheckBox checkbox1 = new CheckBox();
checkbox1.Location = new Point(195, 111);
checkbox1.Text = "C";
//Adding checkbox to form
this.Controls.Add(checkbox1);
CheckBox checkbox2 = new CheckBox();
checkbox2.Location = new Point(195, 156);
checkbox2.Text = "C++";
this.Controls.Add(checkbox2);
CheckBox checkbox3 = new CheckBox();
checkbox3.Location = new Point(195, 195);
checkbox3.Text = "C#";
this.Controls.Add(checkbox3);
CheckBox checkbox4 = new CheckBox();
checkbox4.Location = new Point(195, 235);
checkbox4.Text = "JAVA";
this.Controls.Add(checkbox4);
CheckBox checkbox5 = new CheckBox();
checkbox5.Location = new Point(195, 275);
checkbox5.Text = "HTML";
this.Controls.Add(checkbox5);
}
}
}

Output:

checkbox

Conclusion

A checkbox in C# can also have an indeterminate state. This can be achieved by setting the CheckState property to ‘Indeterminate. It is a state between ‘Yes’ and ‘No’ in which the checkbox will neither be checked nor unchecked.

chckbox new

Recommended Articles

This has been a guide to Checkbox in C#. Here we discuss syntax, CheckBox properties, and CheckBox Events along with its implementation. You may also have a look at the following articles to learn more –

  1. what is Continue in C#
  2. C# Data Types
  3. TextWriter in C#
  4. C# Stack

C# Training Program (6 Courses, 17 Projects)

6 Online Courses

17 Hands-on Project

89+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
C sharp Tutorial
  • Advanced
    • Inheritance in C#
    • Exception Handling in C#
    • Types of Exception in C#
    • C# FileNotFoundException
    • C# NullReferenceException
    • C# OutOfMemoryException
    • C# StackOverflowException
    • Custom Exception in C#
    • What is Multithreading in C#
    • C# finally
    • C# System.IO
    • What is StringBuilder in C#
    • DataReader C#
    • BinaryWriter in C#
    • C# BinaryReader
    • TextWriter in C#
    • TextReader in C#
    • C# StringReader
    • C# StringWriter
    • C# StreamReader
    • C# StreamWriter
    • C# FileInfo
    • What is Design Pattern in C#?
    • Multithreading in C#
    • Sorting in C#
    • Bubble Sort in C#
    • C# SortedList
    • C# SortedSet
    • C# SortedDictionary
    • Abstract Class in C#
    • Access Modifiers in C#
    • C# Generics
    • Deserialization in C#
    • C# Thread
    • C# Thread Join
    • C# Thread Sleep
    • C# Thread Synchronization
    • C# Class
    • Sealed in C#
    • Sealed Class in C#
    • Polymorphism in C#
    • C# Call By Reference
    • Virtual Keyword in C# 
    • Yield Keyword in C#
    • Regular Expression in C#
    • C# Lambda Expression
    • C# Predicate
    • Convert Object to JSON C#
    • Checkbox in C#
    • C# MessageBox
    • Collections in C#
    • List in C#
    • C# LinkedList
    • Listbox in C#
    • Protected in C#
    • C# EventHandler
    • Private in C#
    • this Keyword in C#
    • Static Keyword in C#
    • C# Out Parameter
    • Assert in C#
    • C# Delegates
    • C# Interface
    • Generics in C#
    • Timer in C#
    • C# Serialization
    • Metadata in C#
    • C# Stack
    • C# Using Static
    • Queue in C#
    • C# File.Exists
    • C# Tuples
    • C# Create JSON Object
    • Partial in C#
    • C# readonly
    • C# Action Delegate
    • C# Await Async
    • C# Dictionary
    • IEnumerable C#
    • C# Data Grid View
    • C# Dynamic
    • Web Services in C#
    • C# Pattern Matching
    • C# Extension Methods
    • C# XmlSerializer
  • Basic
    • Uses Of C#
    • C# Versions
    • C# Data Types
    • Variables in C#
    • Namespaces in C#
    • C# Compilers
    • C# Keywords
    • Iterators in C#
    • Objects in C#
    • C# Object Dispose
    • C# object to XML
    • C# check object type
    • C# Object Serialization
    • Pointers in C#
    • C# Literals
    • C# Commands
    • C# Custom Attribute
    • Type Casting in C#
    • String vs String C#
    • C# Struct vs Class
  • Operators
    • Logical Operators in C#
    • Conditional Operators in C#
    • Bitwise Operators in C#
    • C# OR Operator
    • C# Ternary Operators
    • Operator Precedence in C#
  • Control Statement
    • C# if Statement
    • Else If in C#
    • Continue in C#
    • Break in C#
    • Switch Statement in C#
    • Goto Statement in C#
  • Loops
    • C# For Loop
    • C# While Loop
    • C# do-while loop
    • C# foreach Loop
  • Arrays
    • Arrays in C#
    • 2D Arrays in C#
    • C# Jagged Arrays
    • String Array in C#
    • C# Multidimensional Arrays
  • Constructor and Destructor
    • Constructor in C#
    • Copy Constructor in C#
    • Static Constructor in C#
    • Destructor in C#
  • overloading and overrideing
    • Overloading and Overriding in C#
    • Overloading in C#
    • Overriding in C#
    • Method Overloading in C#
    • Method Overriding in C#
    • Operator Overloading in C#
  • Functions
    • C# Functions
    • C# String Functions
    • Math Functions in C#
    • Recursive Function in C#
    • C# Anonymous Functions
    • C# Local Functions
    • Enum in C#
    • Trim() in C#
    • clone() in C#
    • C# random
    • C# String Format()
    • C# String Interpolation
    • C# StartsWith()
    • C# String IndexOf()
    • DateTime in C#
    • C# Nullable
    • C# nameof
    • C# checked
    • C# String PadLeft
    • Convert String to Double in C#
    • Convert int to String C#
    • String to Date C#
    • C# intern()
    • C# Stopwatch
    • C# DirectoryInfo
    • C# Compare()
    • C# Base
    • C# SOAP
    • Lock in C#
  • Programs
    • Patterns in C#
    • Swapping in C#
    • Palindrome in C#
    • Factorial in C#
    • Fibonacci Series in C#
    • Random Number Generator in C#
    • Prime Numbers in C#
    • Armstrong Number in C#
    • Reverse String in C#
  • Interview questions
    • C# Interview Questions and Answers
    • C# OOP Interview Questions
    • C# Design Pattern Interview Questions

Related Courses

C# Certification Training

ASP.NET Course

.NET Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C# Training Program (6 Courses, 17 Projects) Learn More