Home

.NET Controls: Check Boxes

 

Introduction

A check box is a control that makes a statement true or false. To perform this 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.

To let the user know what the check box represents, the control is accompanied by a label that displays the statement. 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.

To create a check box control, you can use the CheckBox class that is implemented by the CheckBox control of the Toolbox. Here is an example:

Imports System
Imports System.Drawing
Imports System.Windows.Forms

Public Class Exercise
    Inherits Form

    Private chkAvailable As CheckBox

    Public Sub New()
        Me.InitializeComponent()
    End Sub
    Sub InitializeComponent()

        chkAvailable = New CheckBox
        chkAvailable.Location = New Point(20, 32)
        chkAvailable.Text = "Available"
        Controls.Add(chkAvailable)

    End Sub

    Public Shared Sub main()
        Dim exo As Exercise
        exo = New Exercise
        Application.Run(exo)
    End Sub
End Class
 

Check Box Characteristics

By default, a check box appears empty, which makes its statement false. To make the statement true, the user clicks it. There are three main ways you can use this property of a check box. To select a check box, you can set its Checked property to True. You can also do it programmatically as follows:

Sub InitializeComponent()

        chkAvailable = New CheckBox
        chkAvailable.Location = New Point(20, 32)
        chkAvailable.Text = "Available"
        chkAvailable.Checked = True
        Controls.Add(chkAvailable)

End Sub

To find out if an item is checked, 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:

chkAvailable.Checked = Not chkAvailable.Checked

By default, the square box of a check box control is positioned to the left side of its accompanying label. 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 which a value from the ContentAlignment enumerator. To programmatically set the check alignment of a check box, you can call the ContentAlignment enumerator and select the desired value. Here is an example:

chkAvailable.CheckAlign = ContentAlignment.MiddleRight

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:

chkAvailable.CheckState = CheckState.Indeterminate

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:

chkAvailable.ThreeState = True

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. You can also do this programmatically as follows:

 

chkAvailable.Appearance = Appearance.Button

Check Box Events

Like the radio button, when a check box is clicked, the control fires a CheckedChanged event to let you know that it has been checked. Once again, 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.

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 box 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.

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

 

Home Copyright © 2005-2012 FunctionX