Chapter 20: List-Based Controls Visual C++ and MFC Fundamentals
20.2.2..Combo Box Properties........................................................................
To create a combo box, you can click the Combo Box button on the Controls
toolbox and click the parent window. After adding a combo box, if you want to
immediately create its strings, in the Properties window, click the Data field. To create
the list of items, type the items separated by a semi-colon
By default, a newly added combo box is of drop down type. The kind of display is
controlled by the Type combo box of the Properties window and its default value is
Dropdown. This is programmatically equivalent to adding the CBS_DROPDOWN
style. A drop down combo box allows the user to type a new value if the desired string is
not in the list. To do this, the user can click the edit side of the control and start typing. If
the user types a string that is longer than the width of the edit box part, the control would
cause the computer to start making a beep sound, indicating that the user cannot type
beyond the allocated length. If you want the user to be able to type a long string, check or
set to True the Auto HScroll property. If you are programmatically creating the control,
you can do this by adding the CBS_AUTOHSCROLL style.
If you do not want the user to be able to type a new string, set the Type property to Drop
List or create the control with a CBS_DROPDOWNLIST style. With this type, whether
the user clicks the edit part or the arrow button, the list would drop. If the user finds the
desired value and clicks it, the list retracts and the new selection displays in the edit part
of the control. If the user does not find the desired item in the list, he or she can click the
edit box or the arrow button. In this case, the selection would not be changed. If you want
the list part of the combo box to always display, like the list box control, set the Type
property to Simple or create it with the CBS_SIMPLE style.
As described above, when the list of items is too long for the reserved rectangular area of
the list side to display, the control gets automatically equipped with a vertical scroll bar.
This is because its Vertical Scroll check box or property is automatically checked or set
to True. If you allow or add this style, if the list is not too long and can be accomodated
by the rectangle, no scroll bar would be displayed. If the list is too long and you set this
property, then a vertical scroll bar would automatically appear on the control. If you insist
on displaying a vertical scroll bar even if the rectangle is long enough to display all items,
check the Disable No Scroll check box or set it to True. In this case, if the rectangle can
accommodate all items, a disabled vertical scroll bar would appear on the control. y either
checking the (or setting it to True) or creating the combo box with the WS_HSCROLL
window style. Based on this convenience, unless you have a strong reason to do
otherwise, you should always allow the vertical scroll bar.
The combo boxes we have mentioned so far are created by their control, in which case
the combo boxes are responsible for creating and updating their list. You can create a
combo box that relies on its owner for all related operations. Such a combo box is
referred to as owner draw. When an owner is responsible for drawing the items of a
combo box, it can set the value of each item as it sees fit. For example, different items of
the same list can use different fonts. They can display different types of items and they
can even draw anything on their line. The ability to feature a combo box as owner draw
or not is specified using the Owner Draw combo box of the Properties window. Its
default value is No. To create an owner draw combo box where all items have the same
height, set this property to the Fixed value. This is equivalent to adding the
CBS_OWNERDRAWFIXED style to a dynamic combo box. On the other hand, if you
want items to have different heights, set the Owner Draw value to Variable or create he
control with a CBS_OWNERDRAWVARIABLE style.