c++winapicomboboxmfc

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


Solution

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