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.

The post How can I have a Win32 drop-down combo box with a read-only edit control? appeared first on The Old New Thing.


From The Old New Thing via this RSS feed