November 28th, 2025
like1 reaction

How can I have a Win32 drop-down combo box with a read-only edit control?

A customer was writing a Win32 program and was looking for something like a combo box, in that it gave the user a choice among various fixed options, but sometimes the program would come up with a new option that wasn’t on the list, and it would want to show that as the selection. On the other hand, the customer didn’t want to give the user the option to enter arbitrary text. Users still had to choose among the fixed items in the combo box or the new option that was generated on the fly. The customer didn’t want to add the new option to the dropdown list, which means that the CBS_DROP­DOWN­LIST style would not be appropriate, since that forces the selection to come from the list. What they wanted was for the combo box to act like a traditional combo box with edit control, except that the user can’t change the text in the edit control.

The solution here is to make the edit control read-only.

You can get the edit control handle by calling Get­Combo­Box­Info and then sending the EM_SET­READ­ONLY message to the window identified by the hwndItem member.

case WM_INITDIALOG:
{
    HWND combo = GetDlgItem(hdlg, IDC_COMBO);
    SendMessage(combo, CB_ADDSTRING, 0, (LPARAM)L"Fixed item 1");
    SendMessage(combo, CB_ADDSTRING, 0, (LPARAM)L"Fixed item 2");
    SendMessage(combo, CB_ADDSTRING, 0, (LPARAM)L"Fixed item 3");

    COMBOBOXINFO info = { sizeof(info) };               
    GetComboBoxInfo(combo, &info);                      
    SendMessage(info.hwndItem, EM_SETREADONLY, TRUE, 0);
}
return TRUE;

The Get­Combo­Box­Info function is admittedly a bit hard to find because it’s not a message or window style, so it’s not in a place that you naturally look when trying to find information about a combo box.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

2 comments

Sort by :
  • GL 38 seconds ago

    What happens if the box displays a special option, the user chooses a fixed item (maybe by using the arrow keys, unaware that the advertised item isn’t in the list), then wants the special option? It appears there’s no way to revert the change, and sounds like it’s going to be used for some sales strategy of pressing people to “take it or leave it”.

  • Kyle Sluder 12 minutes ago

    Acknowledging we don’t have the full context, this seems like a rather strange UI design. If a user selects a different value of the combo box, but then wants to change back to the newly generated one, how do they do that if the program didn’t add the newly generated item to the list? In other words, does the customer have an X-Y problem, where they think their solution requires doing X, but the real problem is the self-imposed constraint Y?