Home

Introduction

 

Fundamentals

 

Introduction

 

 

 
   
 
 

 

 
 
   
 

Previous Copyright © 2015 FunctionX Next

Home

Windows Controls: Checked List Boxes

   

Introduction to the Checked List Boxes

 

Description

A checked list box is a list box whose items are each equipped with a check box. In the following Customize dialog box of Embarcadero RAD Studio, the control under the Toolbars label is a checked list box:

The Customize dialog box

A checked list box combines the functionalities of the list box and the check box controls. As a list box, it displays each of its items on a line. If there are many items than the control can display, it would be equipped with a vertical scroll bar.

To select an item in the list, the user can click the desired string. The most important and obvious characteristic of the checked list box is that each item displays a check box on its left. This box allows the user to select or deselect each item. To select an item, the user must click its box and not its string, to indicate an explicit selection. This draws a check mark in the box. As described with the check box control, the user can deselect an item by removing the check mark. The check mark indicates that an item is selected and the absence of the check mark indicates the contrary. Like the check box control, you can allow the user to indicate a "half-checked" item. In this case, a check box can appear unchecked, checked, or grayed.

Creating a Checked List Box

To support checked list boxes, the .NET Framework provides the CheckedListBox class. To create a checked list box, declare a variable of type CheckedListBox, use the new operator to allocate memory for it, and add it to the Controls collection of its parent. Here is an example:

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

let exercise : Form = new Form()
exercise.Text <- "Personal Relationships"
exercise.StartPosition <- FormStartPosition.CenterScreen

let lbxPersonalRelationships : CheckedListBox = new CheckedListBox()

exercise.Controls.Add(lbxPersonalRelationships)

do
    Application.Run exercise

Characteristics of a Checked List Box

 

Introduction

The CheckedListBox class is derived from the ListBox class. This means that the checked list box possesses all the public (and protected) characteristics of the list box and its parent the ListControl class. This control also uses the HorizontalScrollbar and the HorizontalExtent properties that behave exactly as we reviewed for the list box. It also uses the SelectionMode property with the same behavior as that of the list box.

Creating the List of Items

As seen for the list box, the primary aspect of a checked list box is its list of items. To programmatically create the list of items, access the Items property. The list is created from the nested ObjectCollection class that implements the IList, the ICollection, and the IEnumerable interfaces. This means that the CheckedListBox.ObjectCollection class behaves the same as the ListBox.ObjectCollection class. 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.Text <- "Relations"
exercise.ClientSize <- new System.Drawing.Size(145, 123)
exercise.StartPosition <- FormStartPosition.CenterScreen

let lbxPersonalRelationships : CheckedListBox = new CheckedListBox()
lbxPersonalRelationships.Location <- new Point(12, 12)

lbxPersonalRelationships.Items.Add "Family Member" |> ignore
lbxPersonalRelationships.Items.Add "Friend" |> ignore
lbxPersonalRelationships.Items.Add "Classmate" |> ignore
lbxPersonalRelationships.Items.Add "Business Partner" |> ignore
lbxPersonalRelationships.Items.Add "Simple Acquaintance" |> ignore
lbxPersonalRelationships.Items.Add "Undefined" |> ignore

exercise.Controls.Add(lbxPersonalRelationships)

do
    Application.Run exercise

This would produce:

Creating the List of Items

Remember that you can also add an array of items by calling the AddRange() and you can insert an item using the Insert() method.

Checking an Item

As mentioned already, the main difference between a list box and a checked list is the presence of check marks in the former. When using the control, the user can click one or more check boxes. Here is an example:

Checking an Item

After the user has clicked a few check boxes, you may want to find out which ones are checked. The checked list box provides two techniques you can use.

Each each item of a checked  list box has an index. When one, a few, or all items have been checked (those that display a check mark), the indices of the checked items are stored in a collection represented by the CheckedIndices property. This property is based on the nested CheckedIndexCollection collection class. The CheckedIndexCollection class implements the IList, the ICollection, and the IEnumerable interfaces.

The identifications of the checked items are stored in a collection represented by the CheckedItems property. This property is based on the nested CheckedItemCollection class. The CheckedItemCollection class implements the IList, the ICollection, and the IEnumerable interfaces. This implies that you can use it to get the number of selected items.

When the user has clicked item to put a check mark on it, the control fires the ItemCheck event, which is carried by the ItemCheckEventArgs class.

One of the most important operations to perform on a list of selected items is to find out whether a particular item is checked. To get this information, the CheckedItemCollection class provides a method named Contains. This method takes as argument a value that can identify an item from the checked list box. If the item is found and it is checked, this method returns true. Otherwise, it returns false. 

Automatically Checking an Item When Clicked

From its default implementation, when a checked list box is presented to the user, when an item is clicked, its string gets highlighted but no check mark is put in the box. To put a check mark on the item, the user must click it again. This is not an anomaly. It is purposely so the user would know the difference between an item that is selected and one that is checked.

To support the ability to automatically put a check mark on an item when the user clicks it, the CheckedListBox class provides the CheckOnClick Boolean property. Its default value is false. If you want the items to be automatically checked, set this property to true.

On an existing checked list box, to find out if the its items are automatically checked, get the value of the CheckOnClick property.

3-D Checked Items

After creating the list, each item appears with a flat check box to its left. If you want a 3-D check box, you can change the Boolean ThreeDCheckBoxes property from its false default to a true value:

ThreeDCheckBoxes 
False True 
Checked Box
             

Home Copyright © 2015 FunctionX Home