Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 20: List-Based Controls



  1. Uncheck the Group property of the IDC_RDO_PERSONAL radio button or set this
    property to False

  2. On the Toolbox, click the Static Text button and click on the dialog under the
    group box

  3. Change its Caption to Sample Tables


20.1.2..List Box Fundamentals.......................................................................


To include a list box in your application, from the Controls toolbox, you can click the

List Box button and click on a parent window. The MFC list box is based on the
CListBox class. A newly added list box appears as a rectangular empty box with a white
background.

After creating the list box, the user can select an item by clicking it in the list. The newly
selected item becomes highlighted. If the user clicks another item, the previous one
looses its highlighted color and the new item becomes highlighted. By default, the user
can select only one item at a time. The ability to select one or more items is controlled by
the Selection property whose default value is Single. If you do not want the user to be
able to select any item from the list, you have two main alterrnatives. If you set the
Selection property to None, when the user clicks an item, a rectangle is drawn around the
item but the highlighted color is not set. Alternatively, you can create the control with the
Disabled property set to True.

If you want the user to be able to select more than one item, at design time, set the
Selection property to Multiple. If you are programmatically creating the control, you can
provide this ability by adding the LBS_MULTIPLESEL style. When a list box is
created with the Multiple Selection set, to select an item, the user would click it. To
select an additional item, the user can click it subsequently. If the user clicks an item that
is highlighted, meaning it was already selected, the item becomes selected, loosing its
highlighted color. The user can continue using this feature to build the desired list.

Microsoft Windows allows a user to select items on a list box (in fact on many other list-
based controls or views) by using a combination of mouse click and the keyboard. To
select items in a range, the user can press and hold Shift. Then click an item from the
other range and release Shift. All items between both clicks would be selected. To select
items at random, the user can press and hold Ctrl. Then click each desired item, which
creates a series of cumulative selections. To provide this ability, at design time, you can
set the Selection property to Extended. If you are programmatically creating the list box,
add the LBS_EXTENDEDSEL style to it.

When building the list of items of a list box, by default, the items are rearranged in
alphabetical order. Even if you add an item to an already created list, the new item is
added to the right order. This arrangement means that the list is sorted. The ability to sort
a list box or not is controlled by the LBS_SORT style. If you want the items to keep their
position as they are added, set the Sort property to True.

Practical Learning: Creating List Boxes



  1. On the Toolbox, click the List Box button and click on the dialog under the
    group box.

Free download pdf