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: Introducing Checked List Boxes
|
|
- Start a new Windows Application named AltairRealtors1
- On the main menu, click Project ->Add Class...
- Set the Name to AvailableProperty and click Add
- 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;
}
}
}
|
- In the Solution Explorer, right-click Form1.cs and click Rename
- 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
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: Creating Checked List Boxes
|
|
- Design the form as follows:
 |
| Control |
Text |
Name |
Other Properties |
| Label |
 |
Altair Realtors |
|
BorderStyle: FixedSingle
Font: Times New Roman, 21.75pt, style=Bold |
| Label |
 |
Types to Show |
|
|
| CheckedListBox |
 |
|
lbxPropertiesTypes |
|
| Button |
 |
Show |
btnShow |
|
| Label |
 |
Properties_______ |
|
Font: Garamond, 15.75pt, style=Bold |
| Label |
 |
Prop # |
|
|
| Label |
 |
Address |
|
|
| Label |
 |
City |
|
|
| Label |
 |
State |
|
|
| Label |
 |
ZIP Code |
|
|
| Label |
 |
Beds |
|
|
| Label |
 |
Baths |
|
|
| Label |
 |
Market Value |
|
|
| ListBox |
 |
|
lbxPropertyNumbers |
|
| ListBox |
 |
|
lbxAddresses |
|
| ListBox |
 |
|
lbxCities |
|
| ListBox |
 |
|
lbxStates |
|
| ListBox |
 |
|
lbxZIPCodes |
|
| ListBox |
 |
|
lbxBedrooms |
|
| ListBox |
 |
|
lbxBathrooms |
|
| ListBox |
 |
|
lbxMarketValues |
|
| Button |
 |
Close |
btnClose |
|
|
- Save the form
|
|