Home

Windows Control: 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 of Microsoft Office

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.

Practical Learning Practical Learning: Introducing Checked List Boxes

  1. Start a new Windows Application named AltairRealtors1
  2. On the main menu, click Project ->Add Class...
  3. Set the Name to AvailableProperty and click Add
  4. Change the file as follows:
     
    using System;
    
    namespace AltairRealtors1
    {
        public class AvailableProperty
        {
            long propNbr;
            string propType;
            string adrs;
            string ct;
            string  stt;
            int zip;
            short beds;
            float baths;
            double mValue;
    
            public long PropertyNumber
            {
                get { return propNbr; }
                set { propNbr = value; }
            }
    
            public string PropertyType
            {
                get { return propType; }
                set { propType = value; }
            }
    
            public string Address
            {
                get { return adrs; }
                set { adrs = value; }
            }
    
            public string City
            {
                get { return ct; }
                set { ct = value; }
            }
    
            public string  State
            {
                get { return stt; }
                set { stt = value; }
            }
    
            public int ZIPCode
            {
                get { return zip; }
                set { zip = value; }
            }
    
            public short Bedrooms
            {
                get { return beds; }
                set { beds = value; }
            }
    
            public float Bathrooms
            {
                get { return baths; }
                set { baths = value; }
            }
    
            public double MarketValue
            {
                get { return mValue; }
                set { mValue = value; }
            }
    
            public AvailableProperty()
            {
            }
    
            public AvailableProperty(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;
            }
        }
    }
  5. In the Solution Explorer, right-click Form1.cs and click Rename
  6. Type RealEstate.cs and press Enter twice

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:

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

public class Exercise : System.Windows.Forms.Form
{
    CheckedListBox lbxPersonalRelationships;

    public Exercise()
    {
        InitializeComponent();
    }

    private void InitializeComponent()
    {
        lbxPersonalRelationships = new CheckedListBox();
        lbxPersonalRelationships.Location = new Point(12, 12);

        Controls.Add(lbxPersonalRelationships);
    }
}

public class Program
{
    static int Main()
    {
        System.Windows.Forms.Application.Run(new Exercise());
        return 0;
    }
}

This would produce:

Practical Learning Practical Learning: Creating Checked List Boxes

  1. Design the form as follows:
     
    Altair Realtors
     
    Control Text Name Other Properties
    Label Label Altair Realtors   BorderStyle: FixedSingle
    Font: Times New Roman, 21.75pt, style=Bold
    Label Label Types to Show    
    CheckedListBox   lbxPropertiesTypes  
    Button Show btnShow  
    Label Label Properties_______   Font: Garamond, 15.75pt, style=Bold
    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
 

Home Copyright © 2007 FunctionX, Inc. Next