Reputation: 25286
In Windows C API I have a combobox with dropdown style. I set a text in the edit control of the combo box during the dialog initialization. I want the text to be shown unselected.
I send the following messages:
SendDlgItemMessage(hDlg, IDC_EDIT_FIND, CB_SETCURSEL,0,0);
SendDlgItemMessage(hDlg, IDC_EDIT_FIND, CB_SETEDITSEL,0,MAKELPARAM(-1,0));
but the text is not unselected. The documentation says about CB_SETEDITSEL
:
lParam
[in] The low-order word of lParam specifies the starting position. If the low-order word is –1, the selection, if any, is removed.
The high-order word of lParam specifies the ending position. If the high-order word is –1, all text from the starting position to the last character in the edit control is selected.
And:
If the message succeeds, the return value is TRUE. If the message is sent to a combo box with the CBS_DROPDOWNLIST style, it is CB_ERR.
When I send the message, the result is 1 (TRUE) but the text in the edit control is not unselected
How can I unselect the text of the combobox edit control?
Upvotes: 1
Views: 558
Reputation: 25286
I found it: After WM_INITDIALOG
, Windows sets the focus to the control designated as the first control in the dialog definition, which happened to be the combo box. This caused the focus to be set to the combo box and, no matter how much we have reset it in the WM_INITDIALOG
, the combo text is selected again by the SetFocus.
The solution is to "ignore" this by resetting the selection.
The following is my solution. I use a semaphore to prevent processing of SetFocus messages for the control during the processing of the WM_INITDIALOG
message:
BOOL CALLBACK DlgProcExample (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
static int semaIgnore;
switch (message)
{
case WM_INITDIALOG:
semaIgnore=TRUE;
SendDlgItemMessage(hDlg, IDC_COMBO, CB_RESETCONTENT, 0, 0);
SendDlgItemMessage(hDlg, IDC_COMBO, CB_ADDSTRING,0, (LPARAM)"Hello World");
SendDlgItemMessage(hDlg, IDC_COMBO, CB_SETCURSEL,0,0);
semaIgnore= FALSE;
return (TRUE);
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_COMBO:
if (semaIgnore) break;
switch (HIWORD(wParam)) {
case CBN_SETFOCUS:
SendDlgItemMessage(hDlg, IDC_COMBO, CB_SETEDITSEL,0,MAKELPARAM(-1,99));
break;
}
break;
}
break;
//...
Upvotes: 1