Reputation: 221
I have a simple dialog box with a list box, edit box and two buttons, Send and Clear, Send being the default. When it is pressed the string is read from the edit box and added to the list box. Clear speaks for itself. When I press enter the first time everything works, however when I press enter a second time it doesn't respond/register. If the button is clicked everything works as intended.
I have tried to use DM_SETDEFID after changing the focus back to the edit box, this did nothing.
Stripped it of unnecessary code, so I hope nothing is missing.
INT_PTR CALLBACK CHAT(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDC_BUTTON1)
{
string strDisplay="You: "+strMessage;
TCHAR szDisplay[MESSAGE_SIZE];
strcpy_s(szDisplay, strDisplay.c_str());
SendDlgItemMessage(hDlg, IDC_LIST1, LB_ADDSTRING, NULL, (LPARAM)&szDisplay);
SetDlgItemText(hDlg, IDC_EDIT1, "");
}
SetFocus(GetDlgItem(hDlg, IDC_EDIT1));
}
else if(LOWORD(wParam) == IDC_BUTTON2)
{
SetDlgItemText(hDlg, IDC_EDIT1, "");
}
break;
case WM_CLOSE:
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
}
return (INT_PTR)FALSE;
}
In the resource file:
IDD_CHAT_DIALOG DIALOGEX 0, 0, 309, 176
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Chat"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
LTEXT "Message:",IDC_STATIC,198,12,102,8
EDITTEXT IDC_EDIT1,198,24,102,108,ES_MULTILINE
DEFPUSHBUTTON "Send",IDC_BUTTON1,198,138,104,14
PUSHBUTTON "Clear",IDC_BUTTON2,198,156,104,14
LISTBOX IDC_LIST1,7,7,185,167,LBS_NODATA | LBS_NOSEL | WS_VSCROLL | WS_HSCROLL | WS_TABSTOP
END
Upvotes: 0
Views: 343
Reputation: 612794
You've got a multi-line edit control. As soon as it has the focus, then it will process all presses of the ENTER key.
That's by design. If the button handled presses of the ENTER key when the edit control has focus, how could you enter a new line in the edit control?
If you do need to stop the multi-line edit control from eating the ENTER key then you can handle WM_GETDLGCODE
to arrange that. The technique is explained here: http://blogs.msdn.com/b/oldnewthing/archive/2006/10/12/819674.aspx
As an aside, the strcpy_s is spurious. Call c_str() on your string and pass that directly to the API function.
Upvotes: 6