TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

CheckBox: Windows Forms

This C# tutorial shows how to use the CheckBox control from Windows Forms.

CheckBox has two or three states.

This control provides a way for an option to be selected and deselected independently of other options. As with other controls, it can be manipulated in C# code for custom behaviors.

Start. To get started, you must first add a CheckBox control to your Windows Form in the designer view in Visual Studio. Open the Toolbox pane and then find and double-click on the CheckBox icon. This will insert a CheckBox into your window.

Next: You can add event handlers and change properties by right-clicking on the CheckBox and selecting Properties.

CheckState. It is possible to set the CheckBox to allow three states. These include an indeterminate state as well as checked and unchecked. The next screenshot shows what the indeterminate state looks like when it has been selected in the form.

C# program that uses CheckState and Checked properties

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
	public Form1()
	{
	    InitializeComponent();

	    // Acquire the state of the CheckBox.
	    CheckState state = checkBox1.CheckState;

	    // Demonstrate how the CheckBox can be switched upon.
	    switch (state)
	    {
		case CheckState.Checked:
		case CheckState.Indeterminate:
		case CheckState.Unchecked:
		    {
			MessageBox.Show(state.ToString());
			break;
		    }
	    }

	    // Tell if the box is checked.
	    MessageBox.Show(checkBox1.Checked.ToString());
	}
    }
}

CheckAlign. One of the best things about Windows Forms is that it allows you to change a lot of the styles to suit your tastes. Please remember that if you are a computer programmer your tastes might not match those of a professional designer.

Next: In this screenshot, we see what the CheckBox looks like when I changed the CheckAlign property, and also the FlatStyle property.

CheckChanged. Typically, the CheckBox control is not used as a key control that determines what happens on the Form. The CheckBox state is read when a user performs an action on another control, such as a button.

However, the most useful event is probably CheckChanged. It fires when the user clicks or changes the CheckBox. You can add the CheckChanged event handler by clicking on the lightning bolt and double-clicking on the CheckChanged name.

Event

 

Summary. The CheckBox is useful control in the Windows Forms platform and the C# language. But it is not often a major focus of applications. It is instead a small, supporting control used with other controls.

Review: We described various properties of the CheckBox, which may be useful for planning some programming tasks.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf