8.4.4 Combo Boxes

A combo box is a single control that consists of a list box combined with a static control or edit control. Depending on the style you use to create the list box, the list box can be displayed at all times, or it can be hidden until the user displays it. Except where noted, the mouse and keyboard interface for the edit control and list box of a combo box is identical to that of a standard edit control or list box.

The CBS_SIMPLE style creates a combo box with an edit control and a list box that is always displayed below the edit control. When the combo box has the focus, the user can type in the edit control. If an item in the list box matches what the user has typed, the matching item moves to the top of the list box. The user can also select items from the list box by using the mouse or the DOWN ARROW and UP ARROW keys.

The CBS_DROPDOWN style is similar to CBS_SIMPLE except that the list box is displayed only if the user selects the arrow next to the edit control or presses the ALT+DOWN ARROW or ALT+UP ARROW key combination. Even when the list box is hidden, the user can select items from the list box by using the UP ARROW and DOWN ARROW keys.

A combo box created with the CBS_DROPDOWNLIST appears identical to a CBS_DROPDOWN combo box, except that the edit control is replaced by a static text field. Instead of typing in the edit control, the user can select items from the list box by typing the first letter of the item. The user can also use the mouse or the UP ARROW and DOWN ARROW keys to select items in the combo box.

You add and delete items to the list box portion of a combo box in much the same way you would with a standard list box, but by using the messages CB_ADDSTRING, CB_INSERTSTRING, CB_DELETESTRING, and CB_DIR. Windows also provides additional combo box messages for retrieving the contents of the edit control, matching text with a list box item, and handling the contents of the edit control.

In many respects, a combo box is similar to a list box in the way it reports the user's interaction with the control. All of the list box notification codes have parallel combo box notification codes. In addition to these, Windows sends notification codes to indicate the following:

The list box of the combo box is being dropped down (CBN_DROPDOWN).

The list box of the combo box is being hidden (CBN_CLOSEUP).

The user has changed the text in the edit control, and Windows has updated the screen (CBN_EDITCHANGE).

The user has changed the text in the edit control, but Windows has not yet updated the screen (CBN_EDITUPDATE).

The combo box has lost the input focus (CBN_KILLFOCUS). In the case of CBS_DROPDOWN and CBS_DROPDOWNLIST combo boxes, this causes Windows to remove the list box from the screen.

The combo box has gained the focus (CBN_SETFOCUS).

Like a list box, a combo box can be created with a fixed- or variable-height owner-drawn style. In the case of combo boxes, however, the owner window is responsible for drawing items in the list box and in the edit control. For example, if the user selects an item in the list box, the owner of the combo box receives a WM_DRAWITEM message for the list box item (to draw it as selected) and another WM_DRAWITEM message for the edit control.

You can also specify the CBS_SORT style for a combo box; Windows sorts owner-drawn combo boxes in the same manner as it sorts owner-drawn list boxes.

There is no multicolumn style for combo boxes.