Christian Rau
Christian Rau

Reputation: 45948

Set text in MFC combo box without adding it to the list

I have an MFC combo box, specifically a type derived from CComboBox, created as a dropdown list box without the possibility to edit the selected entry, i.e. a simple list of selectable items.

I do support the possibility of none of the items being selected. I know I can just call SetCurSel(-1) and the edit field of the combo box will be empty until the user selects a "proper" item from the dropdown list. But I would actually prefer to show some kind of default text instead of just an empty field. So what I need to do is set the edit field's text without adding that text to the item list or making the item user-editable.

I tried SetWindowText on the combo box, without success. Based on a comment suggestion I also tried to use GetComboBoxInfo to get a handle to the edit box (in the COMBOBOXINFO::hwndItem) member and calling SetWindowText on that, but this didn't work either (GetComboBoxInfo was successful, though). But I can't imagine this to be a particularly odd use case, so maybe it is possible by other means? If it helps, the combo box is actually ownerdrawn (CBS_OWNERDRAWFIXED).

Upvotes: 1

Views: 2493

Answers (3)

Günter Nagler
Günter Nagler

Reputation: 1

CEdit is a child of CComboBox, you can access the editbox and modify it using (CEdit*)GetChild(GW_CHILD)

Upvotes: -2

IInspectable
IInspectable

Reputation: 51395

The combo box control has builtin support for cue banners. MFC's CComboBox exposes it through the CComboBox::SetCueBanner member:

Cue text is a prompt that is displayed in the input area of the combo box control. The cue text is displayed until the user provides input.

Upvotes: 2

xMRi
xMRi

Reputation: 15365

When you already draw the combobox, than you know that

  1. There is nothing selected if lpdis->itemID == -1
  2. You receive the message when the edit control part has to be drawn. In this case lpdis->itemState has ODS_COMBOBOXEDIT set.

So you are allowed to draw whatever you want.

Upvotes: 1

Related Questions