Home

.NET Controls: Check Boxes

 

Check Box Fundamentals

 

Introduction

The Options dialog box of Microsoft FrontPage

A check box is a control that makes a statement true or false. To perform a validation, this control displays a small square box that the user can click. To start, the square box may be empty . If the user clicks it, a check mark appears in the square box . If the user clicks a check box that has a check mark in it, the check mark may be removed. When the square box is empty *, the statement is false. When the square box is filled with a check mark T, the statement is true.

Creating a Check Box

To support the check box control, the .NET Framework provides the CheckBox class. The CheckBox class is based on the ButtonBase class.

To use a check box in your application, you can declare a CheckBox variable, initialize it using the new operator, and add it to a control container using the Controls.Add() method. Here is an example:
using System;
using System.Drawing;
using System.Windows.Forms;

class Exercise : Form
{
	CheckBox chkValidate;

	public Exercise()
	{
		this.InitializeComponent();
	}

	private void InitializeComponent()
	{
		chkValidate = new CheckBox();
		chkValidate.Location = new Point(32, 20);
		
		this.Controls.Add(chkValidate);
	}

	static int Main()
	{
		Application.Run(new Exercise());
		return 0;
	}
}
 

Characteristics of a Check Box

 

The Caption of a Check Box

To let the user know what the check box control represents, the control is usually accompanied by a label that displays the statement. The specify the caption of this label, simply assign a string to the Text property of the check box control. Here is an example:
using System;
using System.Drawing;
using System.Windows.Forms;

class Exercise : Form
{
	CheckBox chkValidate;

	public Exercise()
	{
		this.InitializeComponent();
	}

	private void InitializeComponent()
	{
		chkValidate = new CheckBox();
                	chkValidate.Text = "Validate";
		chkValidate.Location = new Point(32, 20);
		
		this.Controls.Add(chkValidate);
	}

	static int Main()
	{
		Application.Run(new Exercise());
		return 0;
	}
}
 

The Checked State

By default, a check box appears empty, which makes its statement false. To make the statement true, the user clicks it. This characteristics of the check box is controlled by the Checked Boolean property.

There are three main ways you can use this property of a check box. To select a check box, you can assign a true value to its Checked property. This would be done as follows:

chkValidate.Checked = true;

To find out if an item is selected, get the value of its Checked property. Another possibility consists of toggling the state of the check mark with regards to another action. For example, you can check or uncheck the check mark when the user clicks another button. To do this, you can simply negate the truthfulness of the control as follows:

checkBox1.Checked = !checkBox1.Checked;
 

Caption Alignment

By default, the square box of a check box control is positioned to the left side of its accompanying label. In a .NET Framework application, you have many options. Besides the left position, the most common alignment consists of positioning the square box to the right side of its label. The position of the round box with regards to its label is controlled by the CheckAlign property whose value is based on the ContentAlignment enumerator. To align the caption of a check box, you can use the ContentAlignment enumerator and select the desired value. Here is an example:

chkValidate.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
 

The Intermediate State

Instead of being definitely checked, you can let the user know that the decision of making the statement true or false is not complete. To do this, a check box can display as "half-checked". In this case the check mark would appear as if it were disabled. This behavior is controlled through the CheckState property. To provide this functionality, assign the Indeterminate value to its CheckState property. You can do this programmatically as follows:

chkValidate.CheckState = checkState.Indeterminate;
Indeterminate check box
 

The Three-States of the Check Box

The CheckState property only allows setting the check box control as "undecided". If you actually want the user to control three states of the control as checked, half-checked, or unchecked, use the ThreeState property. By setting the CheckState Boolean property to true, the user can click it two to three times to get the desired value. When this ability is given to the user, you can use or check the value of the Indeterminate property to find out whether the control is checked, half-checked, or unchecked.

Here is an example of specifying the check box control as being able to display one of three states:

chkValidate.Checked = true;
chkValidate.ThreeState = true;
 

Check Box Appearance

By default, a check box control appears as a square box that gets filled with a check mark when the user clicks it. Optionally, you can make a check box control appear as a toggle button. In that case, the button would appear as a regular button. When the user clicks it, it appears down. If the user clicks it again, it becomes up.

To change the appearance of a check box, assign the Button or Normal value to its Appearance property. The Appearance values are defined in the Appearance enumerator. This can be done as follows:

chkValidate.Checked = true;
chkValidate.ThreeState = true;
chkValidate.Appearance = Appearance.Button;
 

Events of a Check Box

 

The Checked Changed

When a check box is clicked, the control fires a CheckedChanged event to let you know that it has been checked. This event is of type EventArgs, meaning that it doesn't give you any more information than the fact that it was clicked. If your check box can display only two states, checked or unchecked, this event is enough.

Here is an example of implementing it:

using System;
using System.Drawing;
using System.Windows.Forms;

class Exercise : Form
{
	CheckBox chkValidate;

	public Exercise()
	{
		this.InitializeComponent();
	}

	private void InitializeComponent()
	{
		chkValidate = new CheckBox();
		chkValidate.Text = "Validate";
		chkValidate.Location = new Point(32, 20);
		chkValidate.CheckedChanged += new EventHandler(chkValidate_CheckedChanged);
		
		this.Controls.Add(chkValidate);
	}

	private void chkValidate_CheckedChanged(object sender, EventArgs e)
	{
		MessageBox.Show("The Checked state has changed");
	}

	static int Main()
	{
		Application.Run(new Exercise());
		return 0;
	}
}
 

The Check-State Changed

If a check box is configured to assume one of three states when it's clicked, that is, if the ThreeState property of a check button is set to True, when the user clicks such a button, the button acquires one of the available three states, which are Checked, Unchecked, or Indeterminate. This causes the control to fire a CheckStateChanged event. This event also is of type EventArgs. This means that it doesn't let you know the current state of the button. You would have to find it out yourself, which is easily done by getting the value of the CheckState property.

Here is an example:

using System;
using System.Drawing;
using System.Windows.Forms;

class Exercise : Form
{
	CheckBox chkValidate;

	public Exercise()
	{
		this.InitializeComponent();
	}

	private void InitializeComponent()
	{
		chkValidate = new CheckBox();
		chkValidate.Text = "Validate";
		chkValidate.Location = new Point(32, 20);
		chkValidate.ThreeState = true;
		chkValidate.CheckStateChanged +=
			new EventHandler(chkValidate_CheckStateChanged);
		
		this.Controls.Add(chkValidate);
	}

	private void chkValidate_CheckStateChanged(object sender, EventArgs e)
	{
		if( chkValidate.CheckState == CheckState.Checked )
			MessageBox.Show("The button has been Checked");
		else if( chkValidate.CheckState == CheckState.Indeterminate )
			MessageBox.Show("The button's checked appearance is Indeterminate");
		else // if( chkValidate.CheckState == CheckState.Unchecked )
			MessageBox.Show("The button is not Checked");
	}

	static int Main()
	{
		Application.Run(new Exercise());
		return 0;
	}
}

The Appearance Changed

The check box control fires an AppearanceChanged event when the button's appearance changes from Normal to Button or vice-versa.

 

Home Copyright © 2004-2010 FunctionX, Inc.