Home

Windows Controls: The Checked List Box

 

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 Microsoft Office, 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 too 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.

ApplicationPractical Learning: Introducing Checked List Boxes

  1. Start Microsoft Visual Studio
  2. To create a new application, on the main menu, click File -> New Project
  3. In the middle list, click Windows Forms Application
  4. Change the Name to RealEstate3
  5. Click OK
  6. On the main menu, click Project ->Add Class...
  7. Make sure C++ Class is selected and click Add
  8. Set the Name to CAvailableProperty
  9. Click Finish
  10. Change the header file as follows:
    #pragma once
    using namespace System;
    
    public ref class CAvailableProperty
    {
    private:
        long propNbr;
        String ^ propType;
        String ^ adrs;
        String ^ ct;
        String ^ stt;
        int zip;
        short beds;
        float baths;
        double mValue;
    public:
    	CAvailableProperty(void);
        CAvailableProperty(long code, String ^ type,
                           String ^ address, String ^ city,
                           String ^ state, int zCode,
                           short bedroom, float bathroom,
                           double value);
        
        property long PropertyNumber
        {
            long get() { return propNbr; }
            void set(long value) { propNbr = value; }
        }
    
        property String ^ PropertyType
        {
            String ^ get() { return propType; }
            void set(String ^ value) { propType = value; }
        }
    
        property String ^ Address
        {
            String ^ get() { return adrs; }
            void set(String ^ value) { adrs = value; }
        }
    
        property String ^ City
        {
            String ^ get() { return ct; }
            void set(String ^ value) { ct = value; }
        }
    
        property String ^ State
        {
            String ^ get() { return stt; }
            void set(String ^ value) { stt = value; }
        }
    
        property int ZIPCode
        {
            int get() { return zip; }
            void set(int value) { zip = value; }
        }
    
        property short Bedrooms
        {
            short get() { return beds; }
            void set(short value) { beds = value; }
        }
    
        property float Bathrooms
        {
            float get() { return baths; }
            void set(float value) { baths = value; }
        }
    
        property double MarketValue
        {
            double get() { return mValue; }
            void set(double value) { mValue = value; }
        }
    };
  11. Change the source file as follows:
    #include "StdAfx.h"
    #include "AvailableProperty.h"
    
    CAvailableProperty::CAvailableProperty(void)
    {
        propNbr  = 0;
        propType = "";
        adrs     = "";
        ct       = "";
        stt      = "";
        zip      = 0;
        beds     = 0;
        baths    = 0.00F;
        mValue   = 0.00;
    }
    
    CAvailableProperty::CAvailableProperty(long code, String ^ type,
                                           String ^ address, String ^ city,
                                           String ^ state, int zCode,
                                           short bedroom, float bathroom,
                                           double value)
    {
        propNbr  = code;
        propType = type;
        adrs     = address;
        ct       = city;
        stt      = state;
        zip      = zCode;
        beds     = bedroom;
        baths    = bathroom;
        mValue   = value;
    }

Creating a Checked List Box

To support checked list boxes, the .NET Framework provides the CheckedListBox class. At design time, to add a checked list box to your application, from the Common Controls section of the Toolbox, click the CheckedListBox button CheckedListBox and click the form or the container that would host the control. To programmatically 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:

#include <windows.h>

#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;

public ref class CExercise : Form
{
private:
    CheckedListBox ^ lbxPersonalRelationships;

public:
	CExercise()
    {
        InitializeComponent();
    }
	
private:
    void InitializeComponent()
    {
        lbxPersonalRelationships = gcnew CheckedListBox;
        lbxPersonalRelationships->Location = Point(12, 12);

	Text = "Checked List Box";
        Controls->Add(lbxPersonalRelationships);
    }
};

[STAThread]
int APIENTRY WinMain(HINSTANCE hInstance,
		     HINSTANCE hPrevInstance,
		     LPSTR lpCmdLine,
		     int nCmdShow)
{
    Application::Run(gcnew CExercise);

    return 0;
}

This would produce:

Checked List Box

ApplicationPractical Learning: Creating Checked List Boxes

  1. Design the form as follows:
     
    Altair Realtors
     
    Control Text Name Other Properties
    Label Label Altair Realtors   AutoSize: False
    BorderStyle: FixedSingle
    Font: Times New Roman, 21.75pt, style=Bold
    ForeColor: Blue
    TextAlign: MiddleCenter
    Label Label Types to Show    
    CheckedListBox   lbxPropertiesTypes  
    Button Show btnShow  
    Label Label Properties_______   Font: Garamond, 15.75pt, style=Bold
    ForeColor: Blue
    Label Label Prop #    
    Label Label Address    
    Label Label City    
    Label Label State    
    Label Label ZIP Code    
    Label Label Beds    
    Label Label Baths    
    Label Label Market Value    
    ListBox ListBox   lbxPropertyNumbers  
    ListBox ListBox   lbxAddresses  
    ListBox ListBox   lbxCities  
    ListBox ListBox   lbxStates  
    ListBox ListBox   lbxZIPCodes  
    ListBox ListBox   lbxBedrooms  
    ListBox ListBox   lbxBathrooms  
    ListBox ListBox   lbxMarketValues  
    Button Button Close btnClose  
  2. Save the form

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. At design time, to create the list of items of a checked list box, access its Properties window, and click the ellipsis button to open the String Collection Editor. Type each item followed by a carriage return. After creating the list, click OK. 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:

void InitializeComponent()
{
        lbxPersonalRelationships = gcnew CheckedListBox;
        lbxPersonalRelationships->Location = Point(12, 12);

        lbxPersonalRelationships->Items->Add("Family Member");
        lbxPersonalRelationships->Items->Add("Friend");
        lbxPersonalRelationships->Items->Add("Classmate");
        lbxPersonalRelationships->Items->Add("Business Partner");
        lbxPersonalRelationships->Items->Add("Simple Acquaintance");
        lbxPersonalRelationships->Items->Add("Undefined");

	Text = "Relationships";
        Controls->Add(lbxPersonalRelationships);
}

This would produce:

Checked List Box

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

ApplicationPractical Learning: Creating a List of Items

  1. On the form, click the checked list box
  2. In the Properties window, click Items and click its ellipsis button
  3. In the String Collection Editor, type Single Families and press Enter
  4. Type Townhouses and press Enter
  5. Type Condominiums and press Enter
  6. Type Trailers
  7. Click OK

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:

Checked List Box

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.

As seen for the list box, each item of the control 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. Its formula is:

System::Void lbxPropertiesTypes_ItemCheck(Object ^ sender, ItemCheckEventArgs ^ e)
{

}

As you can see, the event 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 member function named Contains. Its syntax is:

public:
    virtual bool Contains(Object^ item) sealed;

This member function 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 member function returns true. Otherwise, it returns false.

 
 
 

ApplicationPractical Learning: Using the List of Checked Items

  1. Double-click an unoccupied area of the form to generate its Load event
  2. Change the file as follows:
    #pragma once
    #include "AvailableProperty.h"
    
    namespace RealEstate3 {
    
        . . . No Change
    
        /// <summary>
        /// Summary for Form1
        /// </summary>
        public ref class Form1 : public System::Windows::Forms::Form
        {
        private:
    	/// <summary>
    	/// Required designer variable.
    	/// </summary>
    	System::ComponentModel::Container ^components;
    		
            array<CAvailableProperty ^> ^ properties;
    
    #pragma region Windows Form Designer generated code
    		
    	. . . No Change
    	
    #pragma endregion
    
    System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e)
    {
        properties = gcnew array<CAvailableProperty ^>(15);
    	
        properties[0] = gcnew CAvailableProperty;
        properties[0]->PropertyNumber = 602138;
        properties[0]->PropertyType = "Single Family";
        properties[0]->Address = "11604 Aldora Avenue";
        properties[0]->City = "Baltimore";
        properties[0]->State = "MD";
        properties[0]->ZIPCode = 21205;
        properties[0]->Bedrooms = 5;
        properties[0]->Bathrooms = 3.5F;
        properties[0]->MarketValue = 265880;
    
        properties[1] = gcnew CAvailableProperty;
        properties[1]->PropertyNumber = 749562;
        properties[1]->PropertyType = "Townhouse";
        properties[1]->Address = "495 Parker House Terrace";
        properties[1]->City = "Gettysburg";
        properties[1]->State = "WV";
        properties[1]->ZIPCode = 26201;
        properties[1]->Bedrooms = 3;
        properties[1]->Bathrooms = 2.5F;
        properties[1]->MarketValue = 225500;
    
        properties[2] = gcnew CAvailableProperty;
        properties[2]->PropertyNumber = 304750;
        properties[2]->PropertyType = "Condominium";
        properties[2]->Address = "5900 24th Street NW #812";
        properties[2]->City = "Washington";
        properties[2]->State = "DC";
        properties[2]->ZIPCode = 20008;
        properties[2]->Bedrooms = 1;
        properties[2]->Bathrooms = 1.0F;
        properties[2]->MarketValue = 388665;
    
        properties[3] = gcnew CAvailableProperty;
        properties[3]->PropertyNumber = 682630;
        properties[3]->PropertyType = "Single Family";
        properties[3]->Address = "6114 Costinna Avenue";
        properties[3]->City = "Martinsburg";
        properties[3]->State = "WV";
        properties[3]->ZIPCode = 25401;
        properties[3]->Bedrooms = 4;
        properties[3]->Bathrooms = 3.5F;
        properties[3]->MarketValue = 325000;
    
        properties[4] = gcnew CAvailableProperty;
        properties[4]->PropertyNumber = 480750;
        properties[4]->PropertyType = "Condominium";
        properties[4]->Address = "10710 Desprello Street #10D";
        properties[4]->City = "Rockville";
        properties[4]->State = "MD";
        properties[4]->ZIPCode = 20856;
        properties[4]->Bedrooms = 1;
        properties[4]->Bathrooms = 1.0F;
        properties[4]->MarketValue = 528445;
    
        properties[5] = gcnew CAvailableProperty;
        properties[5]->PropertyNumber = 209475;
        properties[5]->PropertyType = "Single Family";
        properties[5]->Address = "519D Estuardo Way";
        properties[5]->City = "Silver Spring";
        properties[5]->State = "MD";
        properties[5]->ZIPCode = 20906;
        properties[5]->Bedrooms = 2;
        properties[5]->Bathrooms = 1.0F;
        properties[5]->MarketValue = 325995;
               
        properties[6] = gcnew CAvailableProperty;
        properties[6]->PropertyNumber = 304185;
        properties[6]->PropertyType = "Townhouse";
        properties[6]->Address = "10116 Lottsford Drive";
        properties[6]->City = "Takoma Park";
        properties[6]->State = "MD";
        properties[6]->ZIPCode = 20910;
        properties[6]->Bedrooms = 4;
        properties[6]->Bathrooms = 3.5F;
        properties[6]->MarketValue = 450500;
               
        properties[7] = gcnew CAvailableProperty;
        properties[7]->PropertyNumber = 93857;
        properties[7]->PropertyType = "Single Family";
        properties[7]->Address = "9047 Woodyard Road";
        properties[7]->City = "York";
        properties[7]->State = "PA";
        properties[7]->ZIPCode = 17405;
        properties[7]->Bedrooms = 4;
        properties[7]->Bathrooms = 2.5F;
        properties[7]->MarketValue = 326885;
               
        properties[8] = gcnew CAvailableProperty;
        properties[8]->PropertyNumber = 930755;
        properties[8]->PropertyType = "Condominium";
        properties[8]->Address = "3842 Accolade Avenue #1206";
        properties[8]->City = "Alexandria";
        properties[8]->State = "VA";
        properties[8]->ZIPCode = 22231;
        properties[8]->Bedrooms = 3;
        properties[8]->Bathrooms = 2.0F;
        properties[8]->MarketValue = 525885;
                
        properties[9] = gcnew CAvailableProperty;
        properties[9]->PropertyNumber = 240875;
        properties[9]->PropertyType = "Townhouse";
        properties[9]->Address = "842 Hempton Street";
        properties[9]->City = "Charlestown";
        properties[9]->State = "WV";
        properties[9]->ZIPCode = 25414;
        properties[9]->Bedrooms = 3;
        properties[9]->Bathrooms = 2.5F;
        properties[9]->MarketValue = 212500;
                
        properties[10] = gcnew CAvailableProperty;
        properties[10]->PropertyNumber = 940075;
        properties[10]->PropertyType = "Single Family";
        properties[10]->Address = "4813 Woodland Court";
        properties[10]->City = "Falls Church";
        properties[10]->State = "VA";
        properties[10]->ZIPCode = 22042;
        properties[10]->Bedrooms = 5;
        properties[10]->Bathrooms = 3.5F;
        properties[10]->MarketValue = 645860;
                
        properties[11] = gcnew CAvailableProperty;
        properties[11]->PropertyNumber = 931475;
        properties[11]->PropertyType = "Townhouse";
        properties[11]->Address = "5030 Goodson Road";
        properties[11]->City = "Arlington";
        properties[11]->State = "VA";
        properties[11]->ZIPCode = 22203;
        properties[11]->Bedrooms = 4;
        properties[11]->Bathrooms = 3.5F;
        properties[11]->MarketValue = 435775;
                
        properties[12] = gcnew CAvailableProperty;
        properties[12]->PropertyNumber = 248095;
        properties[12]->PropertyType = "Condominium";
        properties[12]->Address = "9182 Weston Ave NW #F14";
        properties[12]->City = "Washington";
        properties[12]->State = "DC";
        properties[12]->ZIPCode = 20016;
        properties[12]->Bedrooms = 2;
        properties[12]->Bathrooms = 1.0F;
        properties[12]->MarketValue = 425665;
                
        properties[13] = gcnew CAvailableProperty;
        properties[13]->PropertyNumber = 293705;
        properties[13]->PropertyType = "Single Family";
        properties[13]->Address = "12404 Livingston Boulevard";
        properties[13]->City = "Martinsburg";
        properties[13]->State = "WV";
        properties[13]->ZIPCode = 25401;
        properties[13]->Bedrooms = 4;
        properties[13]->Bathrooms = 2.50F;
        properties[13]->MarketValue = 225660;
                
        properties[14] = gcnew CAvailableProperty;
        properties[14]->PropertyNumber = 937905;
        properties[14]->PropertyType = "Condominium";
        properties[14]->Address = "2039 Gonnard Road E5";
        properties[14]->City = "Laurel";
        properties[14]->State = "MD";
        properties[14]->ZIPCode = 20747;
        properties[14]->Bedrooms = 2;
        properties[14]->Bathrooms = 2;
        properties[14]->MarketValue = 275880;
    }
    };
    }
  3. Return to the form
  4. On the form, double-click the Show button and implement its Click event as follows:
    System::Void btnShow_Click(System::Object^  sender, System::EventArgs^  e)
    {
        lbxPropertyNumbers->Items->Clear();
        lbxAddresses->Items->Clear();
        lbxCities->Items->Clear();
        lbxStates->Items->Clear();
        lbxZIPCodes->Items->Clear();
        lbxBedrooms->Items->Clear();
        lbxBathrooms->Items->Clear();
        lbxMarketValues->Items->Clear();
    
        if( lbxPropertiesTypes->CheckedItems->Contains("Single Families"))
        {
            for each(CAvailableProperty ^ prop in properties)
    		{
                if( prop->PropertyType == "Single Family")
                {
    		lbxPropertyNumbers->Items->Add(prop->PropertyNumber.ToString());
                    lbxAddresses->Items->Add(prop->Address);
                    lbxCities->Items->Add(prop->City);
                    lbxStates->Items->Add(prop->State);
                    lbxZIPCodes->Items->Add(prop->ZIPCode);
                    lbxBedrooms->Items->Add(prop->Bedrooms);
                    lbxBathrooms->Items->Add(prop->Bathrooms);
                    lbxMarketValues->Items->Add(prop->MarketValue);
                }
            }
        }
    
        if( lbxPropertiesTypes->CheckedItems->Contains("Townhouses"))
        {
            for each(CAvailableProperty ^ prop in properties)
            {
                if( prop->PropertyType == "Townhouse")
                {
                    lbxPropertyNumbers->Items->Add(prop->PropertyNumber.ToString());
                    lbxAddresses->Items->Add(prop->Address);
                    lbxCities->Items->Add(prop->City);
                    lbxStates->Items->Add(prop->State);
                    lbxZIPCodes->Items->Add(prop->ZIPCode);
                    lbxBedrooms->Items->Add(prop->Bedrooms);
                    lbxBathrooms->Items->Add(prop->Bathrooms);
                    lbxMarketValues->Items->Add(prop->MarketValue);
                }
            }
        }
    
        if( lbxPropertiesTypes->CheckedItems->Contains("Condominiums"))
        {
            for each (CAvailableProperty ^ prop in properties)
            {
                if( prop->PropertyType == "Condominium")
                {
                    lbxPropertyNumbers->Items->Add(prop->PropertyNumber.ToString());
                    lbxAddresses->Items->Add(prop->Address);
                    lbxCities->Items->Add(prop->City);
                    lbxStates->Items->Add(prop->State);
                    lbxZIPCodes->Items->Add(prop->ZIPCode);
                    lbxBedrooms->Items->Add(prop->Bedrooms);
                    lbxBathrooms->Items->Add(prop->Bathrooms);
                    lbxMarketValues->Items->Add(prop->MarketValue);
                }
            }
        }
    }
  5. Return to the form and double-click the Close button
  6. Implement its event as follows:
    System::Void btnClose_Click(Object ^ sender, EventArgs ^ e)
    {
        Close();
    }
  7. Execute the application and test the checked list box
     
  8. Close the form and return to your programming environment
  9. To make sure that when a user clicks an item in one list box, the corresponding item gets selected in the other list boxes, on the form, click the Prop # list box
  10. In the Properties window, click the Events button and double-click SelectedIndexChanged
  11. Implement the event as follows:
    System::Void lbxPropertyNumbers_SelectedIndexChanged(System::Object^  sender,
    						     System::EventArgs^  e)
    {
        lbxAddresses->SelectedIndex = lbxPropertyNumbers->SelectedIndex;
        lbxCities->SelectedIndex    = lbxPropertyNumbers->SelectedIndex;
        lbxStates->SelectedIndex    = lbxPropertyNumbers->SelectedIndex;
        lbxZIPCodes->SelectedIndex  = lbxPropertyNumbers->SelectedIndex;
        lbxBedrooms->SelectedIndex  = lbxPropertyNumbers->SelectedIndex;
        lbxBathrooms->SelectedIndex = lbxPropertyNumbers->SelectedIndex;
        lbxMarketValues->SelectedIndex  = lbxPropertyNumbers->SelectedIndex;
    }
  12. Return to the form and click the Address list box
  13. In the Properties window, click the Events button and double-click SelectedIndexChanged
  14. Implement the event as follows:
    System::Void lbxAddresses_SelectedIndexChanged(System::Object^  sender,
    						System::EventArgs^  e)
    {
        lbxPropertyNumbers->SelectedIndex = lbxAddresses->SelectedIndex;
        lbxCities->SelectedIndex    = lbxAddresses->SelectedIndex;
        lbxStates->SelectedIndex    = lbxAddresses->SelectedIndex;
        lbxZIPCodes->SelectedIndex  = lbxAddresses->SelectedIndex;
        lbxBedrooms->SelectedIndex  = lbxAddresses->SelectedIndex;
        lbxBathrooms->SelectedIndex = lbxAddresses->SelectedIndex;
        lbxMarketValues->SelectedIndex  = lbxAddresses->SelectedIndex;
    }
  15. Return to the form and click the City list box
  16. In the Properties window, click the Events button and double-click SelectedIndexChanged
  17. Implement the event as follows:
    System::Void lbxCities_SelectedIndexChanged(System::Object^  sender,
    					    System::EventArgs^  e)
    {
        lbxPropertyNumbers->SelectedIndex = lbxCities->SelectedIndex;
        lbxAddresses->SelectedIndex = lbxCities->SelectedIndex;
        lbxStates->SelectedIndex    = lbxCities->SelectedIndex;
        lbxZIPCodes->SelectedIndex  = lbxCities->SelectedIndex;
        lbxBedrooms->SelectedIndex  = lbxCities->SelectedIndex;
        lbxBathrooms->SelectedIndex = lbxCities->SelectedIndex;
        lbxMarketValues->SelectedIndex  = lbxCities->SelectedIndex;
    }
  18. Return to the form and click the State list box
  19. In the Properties window, click the Events button and double-click SelectedIndexChanged
  20. Implement the event as follows:
    System::Void lbxStates_SelectedIndexChanged(System::Object^  sender, 
    						System::EventArgs^  e)
    {
        lbxPropertyNumbers->SelectedIndex = lbxStates->SelectedIndex;
        lbxAddresses->SelectedIndex = lbxStates->SelectedIndex;
        lbxCities->SelectedIndex = lbxStates->SelectedIndex;
        lbxZIPCodes->SelectedIndex  = lbxStates->SelectedIndex;
        lbxBedrooms->SelectedIndex  = lbxStates->SelectedIndex;
        lbxBathrooms->SelectedIndex = lbxStates->SelectedIndex;
        lbxMarketValues->SelectedIndex  = lbxStates->SelectedIndex;
    }
  21. Return to the form and click the ZIP Code list box
  22. In the Properties window, click the Events button and double-click SelectedIndexChanged
  23. Implement the event as follows:
    System::Void lbxZIPCodes_SelectedIndexChanged(System::Object^  sender, 
    						System::EventArgs^  e)
    {
        lbxPropertyNumbers->SelectedIndex = lbxZIPCodes->SelectedIndex;
        lbxStates->SelectedIndex = lbxZIPCodes->SelectedIndex;
        lbxAddresses->SelectedIndex = lbxZIPCodes->SelectedIndex;
        lbxCities->SelectedIndex = lbxZIPCodes->SelectedIndex;
        lbxBedrooms->SelectedIndex  = lbxZIPCodes->SelectedIndex;
        lbxBathrooms->SelectedIndex = lbxZIPCodes->SelectedIndex;
        lbxMarketValues->SelectedIndex  = lbxZIPCodes->SelectedIndex;
    }
  24. Return to the form and click the Beds list box
  25. In the Properties window, click the Events button and double-click SelectedIndexChanged
  26. Implement the event as follows:
    System::Void lbxBedrooms_SelectedIndexChanged(System::Object^  sender, 
    						System::EventArgs^  e)
    {
        lbxPropertyNumbers->SelectedIndex = lbxBedrooms->SelectedIndex;
        lbxAddresses->SelectedIndex = lbxBedrooms->SelectedIndex;
        lbxCities->SelectedIndex = lbxBedrooms->SelectedIndex;
        lbxStates->SelectedIndex = lbxBedrooms->SelectedIndex;
        lbxZIPCodes->SelectedIndex = lbxBedrooms->SelectedIndex;
        lbxBathrooms->SelectedIndex = lbxBedrooms->SelectedIndex;
        lbxMarketValues->SelectedIndex  = lbxBedrooms->SelectedIndex;
    }
  27. Return to the form and click the Baths list box
  28. In the Properties window, click the Events button and double-click SelectedIndexChanged
  29. Implement the event as follows:
    System::Void lbxBathrooms_SelectedIndexChanged(System::Object^  sender,
    						 System::EventArgs^  e)
    {
        lbxPropertyNumbers->SelectedIndex = lbxBathrooms->SelectedIndex;
        lbxAddresses->SelectedIndex = lbxBathrooms->SelectedIndex;
        lbxCities->SelectedIndex = lbxBathrooms->SelectedIndex;
        lbxStates->SelectedIndex = lbxBathrooms->SelectedIndex;
        lbxZIPCodes->SelectedIndex = lbxBathrooms->SelectedIndex;
        lbxBedrooms->SelectedIndex = lbxBathrooms->SelectedIndex;
        lbxMarketValues->SelectedIndex  = lbxBathrooms->SelectedIndex;
    }
  30. Return to the form and click the Market Value list box
  31. In the Properties window, click the Events button and double-click SelectedIndexChanged
  32. Implement the event as follows:
    System::Void lbxMarketValues_SelectedIndexChanged(System::Object^  sender,
    						  System::EventArgs^  e)
    {
        lbxPropertyNumbers->SelectedIndex = lbxMarketValues->SelectedIndex;
        lbxAddresses->SelectedIndex = lbxMarketValues->SelectedIndex;
        lbxCities->SelectedIndex = lbxMarketValues->SelectedIndex;
        lbxStates->SelectedIndex = lbxMarketValues->SelectedIndex;
        lbxZIPCodes->SelectedIndex = lbxMarketValues->SelectedIndex;
        lbxBedrooms->SelectedIndex = lbxMarketValues->SelectedIndex;
        lbxBathrooms->SelectedIndex  = lbxMarketValues->SelectedIndex;
    }
  33. Return to the form
  34. Double-click the Close button
  35. Implement the event as follows:
    System::Void btnClose_Click(System::Object^  sender, System::EventArgs^  e)
    {
    	Close();
    }
  36. Return to the form
  37. On the form, click the checked list box
  38. In the Properties window, click the Properties button

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

Application Topic Applied: Automatically Checking an Item

  1. Double-click CheckOnClick to set its value to True
  2. Execute the application to test it
  3. Close the form and return to your programming environment

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 © 2011 FunctionX, Inc. Home