Home

Introduction

 

Fundamentals

 

Introduction

 

 

 
   
 
 

 

 
 
   
 

Previous Copyright © 2015 FunctionX Next

Home

Button-Based Controls: Check Boxes

   

Overview of Check Boxes

 

Introduction

A check box is a control that makes a statement true or false. To perform this validation, the control displays a small square box that the user can click. Here are examples:

The Options dialog box of Microsoft FrontPage

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 control 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 displays a check mark , the statement is true.

Creating a Check Box

To support check boxes, the .NET Framework provides the CheckBox class. To add a check box to your application at design time, from the Common Controls section of the Toolbox, yon can click the CheckBox control and click the form or a container on the form. Unlike the radio button but like the regular command button, a check box can safely be positioned directly on a form.

To programmatically create a check box, declare a variable of type CheckBox, use the new operator to allocate memory for it, and add it to the Controls collection of its holder. Here is an example:

open System
open System.Windows.Forms

let exercise : System.Windows.Forms.Form = new  System.Windows.Forms.Form()

let chkValidate : CheckBox = new CheckBox()

Controls.Add(chkValidate)

Application.Run(exercise)

Unlike the radio button that usually comes along with other radio buttons, a check box can appear by itself. Even when it comes in a group with others, the behavior of one check box is independent of the other check boxes, even if they belong to the same group.

Check Box Characteristics

 

Checking a Check Box

By default, a check box appears empty, which makes its statement false. To make the statement true, the user can click 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:

open System
open System.Drawing
open System.Windows.Forms

let exercise : System.Windows.Forms.Form = new  System.Windows.Forms.Form()

let chkValidate : CheckBox = new CheckBox()

exercise.Controls.Add(chkValidate)

let FormLoading(e : EventArgs) =
    chkValidate.Checked <- true

exercise.Load.Add FormLoading

Application.Run(exercise)

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

open System
open System.Drawing
open System.Windows.Forms

let exercise : System.Windows.Forms.Form = new  System.Windows.Forms.Form()
exercise.ClientSize <- new Drawing.Size(140, 50)
exercise.Text <- "Validation"

let btnValidate = new Button()
exercise.MaximizeBox <- false
btnValidate.Text <- "Validate"
exercise.Controls.Add btnValidate
btnValidate.Location <- new Point(12, 12)

let chkValidate : CheckBox = new CheckBox()
exercise.Controls.Add(chkValidate)
chkValidate.Location <- new Point(100, 17)
exercise.StartPosition <- FormStartPosition.CenterScreen

let FormLoading(e : EventArgs) =
    chkValidate.Checked <- true

let btnValidateClick e =
    chkValidate.Checked <- not chkValidate.Checked

btnValidate.Click.Add btnValidateClick

exercise.Load.Add FormLoading

do
    Application.Run exercise

Checking a Check Box

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. If the check box can display only two states, checked or unchecked, this event is enough.

The Alignment of a Check Box

By default, the square box of a check control is positioned to the left side of its accompanying label. Instead of this default left position, you can change it as you wish. The position of the square box with regards to its label is controlled by the CheckAlign property that is of type ContentAlignment. To programmatically set the check alignment of a check box, you can call the ContentAlignment enumeration and select the desired value. Here is an example:

open System
open System.Drawing
open System.Windows.Forms

let exercise : System.Windows.Forms.Form = new  System.Windows.Forms.Form()
exercise.ClientSize <- new Drawing.Size(140, 50)
exercise.Text <- "Validation"

let chkValidate : CheckBox = new CheckBox()
chkValidate.Text <- "Validate"
exercise.Controls.Add(chkValidate)
chkValidate.Location <- new Point(12, 12)

chkValidate.CheckAlign <- ContentAlignment.MiddleRight

exercise.StartPosition <- FormStartPosition.CenterScreen

do
    Application.Run exercise

The Alignment of a Check Box

The Checked State of a Check Box

Instead of being definitely checked, you can let the user know that the decision of making the statement true or false is not complete. This means that 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:

open System
open System.Drawing
open System.Windows.Forms

let exercise : System.Windows.Forms.Form = new  System.Windows.Forms.Form()
exercise.ClientSize <- new Drawing.Size(140, 50)
exercise.Text <- "Validation"

let chkValidate : CheckBox = new CheckBox()
chkValidate.Text <- "Validate"
exercise.Controls.Add(chkValidate)
chkValidate.Location <- new Point(12, 12)

chkValidate.CheckState <- CheckState.Indeterminate

chkValidate.CheckAlign <- ContentAlignment.MiddleRight
exercise.StartPosition <- FormStartPosition.CenterScreen

do
    Application.Run exercise

The Checked State of a 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:

open System
open System.Drawing
open System.Windows.Forms

let exercise : System.Windows.Forms.Form = new  System.Windows.Forms.Form()
exercise.Text <- "Validation"
exercise.ClientSize <- new Drawing.Size(140, 50)
exercise.StartPosition <- FormStartPosition.CenterScreen

let chkValidate : CheckBox = new CheckBox()
chkValidate.Text <- "Validate"
exercise.Controls.Add(chkValidate)
chkValidate.Location <- new Point(12, 12)

chkValidate.Checked  <- true
chkValidate.ThreeState <- true
chkValidate.CheckAlign <- ContentAlignment.MiddleRight

do
    Application.Run exercise

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 does not 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.

The Appearance of a Check Box

By default, a check box appears as a square box that gets a check mark when the user clicks it. Optionally, you can make a check box 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 enumeration. You can also do this programmatically as follows:

open System
open System.Drawing
open System.Windows.Forms

let exercise : System.Windows.Forms.Form = new  System.Windows.Forms.Form()
exercise.Text <- "Validation"
exercise.ClientSize <- new Drawing.Size(140, 50)
exercise.StartPosition <- FormStartPosition.CenterScreen

let chkValidate : CheckBox = new CheckBox()
chkValidate.Text <- "Validate"
exercise.Controls.Add(chkValidate)
chkValidate.Location <- new Point(12, 12)

chkValidate.Checked  <- true
chkValidate.ThreeState <- true
chkValidate.Appearance <- Appearance.Button

do
    Application.Run exercise

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 © 2015 FunctionX Home