1
votes

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).

2
are you try call GetComboBoxInfo and then use COMBOBOXINFO.hwndItem in call SetWindowText ?RbMm
@RbMm Thanks for the suggestion. But I just tried it and it doesn't seem to work. Maybe because it's ownerdraw?Christian Rau
IIRC (and maybe I don't) the edit box is passed to WM_DRAWITEM as itemID - 1. Something like that anyway, put a breakpoint on it and check.Paul Sanders

2 Answers

2
votes

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.

2
votes

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.