search

Checkbox in C#

Sep 03, 2024 pm 03:27 PM
c#c# tutorial

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.

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”;

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 in C#

Conclusion – Checkbox in C#

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.

Checkbox in C#

The above is the detailed content of Checkbox in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Mastering C# .NET Design Patterns: From Singleton to Dependency InjectionMastering C# .NET Design Patterns: From Singleton to Dependency InjectionMay 09, 2025 am 12:15 AM

Design patterns in C#.NET include Singleton patterns and dependency injection. 1.Singleton mode ensures that there is only one instance of the class, which is suitable for scenarios where global access points are required, but attention should be paid to thread safety and abuse issues. 2. Dependency injection improves code flexibility and testability by injecting dependencies. It is often used for constructor injection, but it is necessary to avoid excessive use to increase complexity.

C# .NET in the Modern World: Applications and IndustriesC# .NET in the Modern World: Applications and IndustriesMay 08, 2025 am 12:08 AM

C#.NET is widely used in the modern world in the fields of game development, financial services, the Internet of Things and cloud computing. 1) In game development, use C# to program through the Unity engine. 2) In the field of financial services, C#.NET is used to develop high-performance trading systems and data analysis tools. 3) In terms of IoT and cloud computing, C#.NET provides support through Azure services to develop device control logic and data processing.

C# .NET Framework vs. .NET Core/5/6: What's the Difference?C# .NET Framework vs. .NET Core/5/6: What's the Difference?May 07, 2025 am 12:06 AM

.NETFrameworkisWindows-centric,while.NETCore/5/6supportscross-platformdevelopment.1).NETFramework,since2002,isidealforWindowsapplicationsbutlimitedincross-platformcapabilities.2).NETCore,from2016,anditsevolutions(.NET5/6)offerbetterperformance,cross-

The Community of C# .NET Developers: Resources and SupportThe Community of C# .NET Developers: Resources and SupportMay 06, 2025 am 12:11 AM

The C#.NET developer community provides rich resources and support, including: 1. Microsoft's official documents, 2. Community forums such as StackOverflow and Reddit, and 3. Open source projects on GitHub. These resources help developers improve their programming skills from basic learning to advanced applications.

The C# .NET Advantage: Features, Benefits, and Use CasesThe C# .NET Advantage: Features, Benefits, and Use CasesMay 05, 2025 am 12:01 AM

The advantages of C#.NET include: 1) Language features, such as asynchronous programming simplifies development; 2) Performance and reliability, improving efficiency through JIT compilation and garbage collection mechanisms; 3) Cross-platform support, .NETCore expands application scenarios; 4) A wide range of practical applications, with outstanding performance from the Web to desktop and game development.

Is C# Always Associated with .NET? Exploring AlternativesIs C# Always Associated with .NET? Exploring AlternativesMay 04, 2025 am 12:06 AM

C# is not always tied to .NET. 1) C# can run in the Mono runtime environment and is suitable for Linux and macOS. 2) In the Unity game engine, C# is used for scripting and does not rely on the .NET framework. 3) C# can also be used for embedded system development, such as .NETMicroFramework.

The .NET Ecosystem: C#'s Role and BeyondThe .NET Ecosystem: C#'s Role and BeyondMay 03, 2025 am 12:04 AM

C# plays a core role in the .NET ecosystem and is the preferred language for developers. 1) C# provides efficient and easy-to-use programming methods, combining the advantages of C, C and Java. 2) Execute through .NET runtime (CLR) to ensure efficient cross-platform operation. 3) C# supports basic to advanced usage, such as LINQ and asynchronous programming. 4) Optimization and best practices include using StringBuilder and asynchronous programming to improve performance and maintainability.

C# as a .NET Language: The Foundation of the EcosystemC# as a .NET Language: The Foundation of the EcosystemMay 02, 2025 am 12:01 AM

C# is a programming language released by Microsoft in 2000, aiming to combine the power of C and the simplicity of Java. 1.C# is a type-safe, object-oriented programming language that supports encapsulation, inheritance and polymorphism. 2. The compilation process of C# converts the code into an intermediate language (IL), and then compiles it into machine code execution in the .NET runtime environment (CLR). 3. The basic usage of C# includes variable declarations, control flows and function definitions, while advanced usages cover asynchronous programming, LINQ and delegates, etc. 4. Common errors include type mismatch and null reference exceptions, which can be debugged through debugger, exception handling and logging. 5. Performance optimization suggestions include the use of LINQ, asynchronous programming, and improving code readability.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)