Share via


CComboBox Class

Provides the functionality of a Windows combo box.

class CComboBox : public CWnd

Members

Public Constructors

Name

Description

CComboBox::CComboBox

Constructs a CComboBox object.

Public Methods

Name

Description

CComboBox::AddString

Adds a string to the end of the list in the list box of a combo box, or at the sorted position for list boxes with the CBS_SORT style.

CComboBox::Clear

Deletes (clears) the current selection, if any, in the edit control.

CComboBox::CompareItem

Called by the framework to determine the relative position of a new list item in a sorted owner-drawn combo box.

CComboBox::Copy

Copies the current selection, if any, onto the Clipboard in CF_TEXT format.

CComboBox::Create

Creates the combo box and attaches it to the CComboBox object.

CComboBox::Cut

Deletes (cuts) the current selection, if any, in the edit control and copies the deleted text onto the Clipboard in CF_TEXT format.

CComboBox::DeleteItem

Called by the framework when a list item is deleted from an owner-drawn combo box.

CComboBox::DeleteString

Deletes a string from the list box of a combo box.

CComboBox::Dir

Adds a list of file names to the list box of a combo box.

CComboBox::DrawItem

Called by the framework when a visual aspect of an owner-drawn combo box changes.

CComboBox::FindString

Finds the first string that contains the specified prefix in the list box of a combo box.

CComboBox::FindStringExact

Finds the first list-box string (in a combo box) that matches the specified string.

CComboBox::GetComboBoxInfo

Retrieves information about the CComboBox object.

CComboBox::GetCount

Retrieves the number of items in the list box of a combo box.

CComboBox::GetCueBanner

Gets the cue text that is displayed for a combo box control.

CComboBox::GetCurSel

Retrieves the index of the currently selected item, if any, in the list box of a combo box.

CComboBox::GetDroppedControlRect

Retrieves the screen coordinates of the visible (dropped down) list box of a drop-down combo box.

CComboBox::GetDroppedState

Determines whether the list box of a drop-down combo box is visible (dropped down).

CComboBox::GetDroppedWidth

Retrieves the minimum allowed width for the drop-down list-box portion of a combo box.

CComboBox::GetEditSel

Gets the starting and ending character positions of the current selection in the edit control of a combo box.

CComboBox::GetExtendedUI

Determines whether a combo box has the default user interface or the extended user interface.

CComboBox::GetHorizontalExtent

Returns the width in pixels that the list-box portion of the combo box can be scrolled horizontally.

CComboBox::GetItemData

Retrieves the application-supplied 32-bit value associated with the specified combo-box item.

CComboBox::GetItemDataPtr

Retrieves the application-supplied 32-bit pointer that is associated with the specified combo-box item.

CComboBox::GetItemHeight

Retrieves the height of list items in a combo box.

CComboBox::GetLBText

Gets a string from the list box of a combo box.

CComboBox::GetLBTextLen

Gets the length of a string in the list box of a combo box.

CComboBox::GetLocale

Retrieves the locale identifier for a combo box.

CComboBox::GetMinVisible

Gets the minimum number of visible items in the drop-down list of the current combo box.

CComboBox::GetTopIndex

Returns the index of the first visible item in the list-box portion of the combo box.

CComboBox::InitStorage

Preallocates blocks of memory for items and strings in the list-box portion of the combo box.

CComboBox::InsertString

Inserts a string into the list box of a combo box.

CComboBox::LimitText

Limits the length of the text that the user can enter into the edit control of a combo box.

CComboBox::MeasureItem

Called by the framework to determine combo box dimensions when an owner-drawn combo box is created.

CComboBox::Paste

Inserts the data from the Clipboard into the edit control at the current cursor position. Data is inserted only if the Clipboard contains data in CF_TEXT format.

CComboBox::ResetContent

Removes all items from the list box and edit control of a combo box.

CComboBox::SelectString

Searches for a string in the list box of a combo box and, if the string is found, selects the string in the list box and copies the string to the edit control.

CComboBox::SetCueBanner

Sets the cue text that is displayed for a combo box control.

CComboBox::SetCurSel

Selects a string in the list box of a combo box.

CComboBox::SetDroppedWidth

Sets the minimum allowed width for the drop-down list-box portion of a combo box.

CComboBox::SetEditSel

Selects characters in the edit control of a combo box.

CComboBox::SetExtendedUI

Selects either the default user interface or the extended user interface for a combo box that has the CBS_DROPDOWN or CBS_DROPDOWNLIST style.

CComboBox::SetHorizontalExtent

Sets the width in pixels that the list-box portion of the combo box can be scrolled horizontally.

CComboBox::SetItemData

Sets the 32-bit value associated with the specified item in a combo box.

CComboBox::SetItemDataPtr

Sets the 32-bit pointer associated with the specified item in a combo box.

CComboBox::SetItemHeight

Sets the height of list items in a combo box or the height of the edit-control (or static-text) portion of a combo box.

CComboBox::SetLocale

Sets the locale identifier for a combo box.

CComboBox::SetMinVisibleItems

Sets the minimum number of visible items in the drop-down list of the current combo box.

CComboBox::SetTopIndex

Tells the list-box portion of the combo box to display the item with the specified index at the top.

CComboBox::ShowDropDown

Shows or hides the list box of a combo box that has the CBS_DROPDOWN or CBS_DROPDOWNLIST style.

Remarks

A combo box consists of a list box combined with either a static control or edit control. The list-box portion of the control may be displayed at all times or may only drop down when the user selects the drop-down arrow next to the control.

The currently selected item (if any) in the list box is displayed in the static or edit control. In addition, if the combo box has the drop-down list style, the user can type the initial character of one of the items in the list, and the list box, if visible, will highlight the next item with that initial character.

The following table compares the three combo-box styles.

Style

When is list box visible?

Static or edit control?

Simple

Always

Edit

Drop-down

When dropped down

Edit

Drop-down list

When dropped down

Static

You can create a CComboBox object from either a dialog template or directly in your code. In both cases, first call the constructor CComboBox to construct the CComboBox object; then call the Create member function to create the control and attach it to the CComboBox object.

If you want to handle Windows notification messages sent by a combo box to its parent (usually a class derived from CDialog), add a message-map entry and message-handler member function to the parent class for each message.

Each message-map entry takes the following form:

ON_Notification( id**,** memberFxn )

where id specifies the child-window ID of the combo-box control sending the notification and memberFxn is the name of the parent member function you have written to handle the notification.

The parent's function prototype is as follows:

afx_msg void memberFxn**(** );

The order in which certain notifications will be sent cannot be predicted. In particular, a CBN_SELCHANGE notification may occur either before or after a CBN_CLOSEUP notification.

Potential message-map entries are the following:

  • ON_CBN_CLOSEUP   (Windows 3.1 and later.) The list box of a combo box has closed. This notification message is not sent for a combo box that has the CBS_SIMPLE style.

  • ON_CBN_DBLCLK   The user double-clicks a string in the list box of a combo box. This notification message is only sent for a combo box with the CBS_SIMPLE style. For a combo box with the CBS_DROPDOWN or CBS_DROPDOWNLIST style, a double-click cannot occur because a single click hides the list box.

  • ON_CBN_DROPDOWN   The list box of a combo box is about to drop down (be made visible). This notification message can occur only for a combo box with the CBS_DROPDOWN or CBS_DROPDOWNLIST style.

  • ON_CBN_EDITCHANGE   The user has taken an action that may have altered the text in the edit-control portion of a combo box. Unlike the CBN_EDITUPDATE message, this message is sent after Windows updates the screen. It is not sent if the combo box has the CBS_DROPDOWNLIST style.

  • ON_CBN_EDITUPDATE   The edit-control portion of a combo box is about to display altered text. This notification message is sent after the control has formatted the text but before it displays the text. It is not sent if the combo box has the CBS_DROPDOWNLIST style.

  • ON_CBN_ERRSPACE   The combo box cannot allocate enough memory to meet a specific request.

  • ON_CBN_SELENDCANCEL   (Windows 3.1 and later.) Indicates the user's selection should be canceled. The user clicks an item and then clicks another window or control to hide the list box of a combo box. This notification message is sent before the CBN_CLOSEUP notification message to indicate that the user's selection should be ignored. The CBN_SELENDCANCEL or CBN_SELENDOK notification message is sent even if the CBN_CLOSEUP notification message is not sent (as in the case of a combo box with the CBS_SIMPLE style).

  • ON_CBN_SELENDOK   The user selects an item and then either presses the ENTER key or clicks the DOWN ARROW key to hide the list box of a combo box. This notification message is sent before the CBN_CLOSEUP message to indicate that the user's selection should be considered valid. The CBN_SELENDCANCEL or CBN_SELENDOK notification message is sent even if the CBN_CLOSEUP notification message is not sent (as in the case of a combo box with the CBS_SIMPLE style).

  • ON_CBN_KILLFOCUS   The combo box is losing the input focus.

  • ON_CBN_SELCHANGE   The selection in the list box of a combo box is about to be changed as a result of the user either clicking in the list box or changing the selection by using the arrow keys. When processing this message, the text in the edit control of the combo box can only be retrieved via GetLBText or another similar function. GetWindowText cannot be used.

  • ON_CBN_SETFOCUS   The combo box receives the input focus.

If you create a CComboBox object within a dialog box (through a dialog resource), the CComboBox object is automatically destroyed when the user closes the dialog box.

If you embed a CComboBox object within another window object, you do not need to destroy it. If you create the CComboBox object on the stack, it is destroyed automatically. If you create the CComboBox object on the heap by using the new function, you must call delete on the object to destroy it when the Windows combo box is destroyed.

Note   If you want to handle WM_KEYDOWN and WM_CHAR messages, you have to subclass the combo box's edit and list box controls, derive classes from CEdit and CListBox, and add handlers for those messages to the derived classes. For more information, see https://support.microsoft.com/default.aspx?scid=kb;en-us;Q174667 and CWnd::SubclassWindow.

Inheritance Hierarchy

CObject

CCmdTarget

CWnd

CComboBox

Requirements

Header: afxwin.h

See Also

Reference

CWnd Class

Hierarchy Chart

CWnd Class

CButton Class

CEdit Class

CListBox Class

CScrollBar Class

CStatic Class

CDialog Class

Concepts

MFC Sample CTRLBARS