-
CEdit and Return Key
Hi,
I have an EditBox and a button control. I want changes in editBox to be applied (but calling OnButtonClick)
when the user hits the return key. The problem is that, as the Ok button is the default button, the dialog is closed
as soon as we press enter. I tried to change the button style (using BS_DEFPUSHBUTTON) but this doesn't
work. I tried also to subclass the edit control and trap the VK_RETURN in the onChar() member function but
this doesn't work either.
Can you help me?
--
Where there is a WISH, there is a WILL.
-
Re: CEdit and Return Key
Override PreTranslateMessage and add the following code
BOOL YourDialog::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN)
{
if (pMsg->wParam == VK_ENTER)
{
OnBtnClicked(); // you could also use post message but this works
return TRUE;
}
}
return yourbaseclass::PreTranslateMessage(pMsg);
}
HTH,
Chris
-
Re: CEdit and Return Key
Sorry,
you need to test for the edit control being the window
so make the following change to the function
if (pMsg->wParam == VK_ENTER)
{
if (pMsg->hwnd == ::GetDlgItem(IDC_EDIT))
{
OnBtnClicked(); // you could also use post message but this works
return TRUE;
}
}
HTH,
Chris
-
Re: CEdit and Return Key
Thanks. The only problem is that ::GetDlgItem does not take one parameter and and don't know how to obtain the handle of a control (or dialog).
--
Where there is a WISH, there is a WILL.
-
Re: CEdit and Return Key
Try GetDlgItem without the scope resolution operator (::). Much simpler.
GetDlgItem(IDC_EDIT);
If, however, you must use ::GetDlgItem, you can get the HWND by calling GetSafeHwnd.
The code would then look like this:
::GetDlgItem(GetSafeHwnd(), IDC_EDIT);
-
Re: CEdit and Return Key
Thank you Rabit-toaster, thank you ChriD. I thank you a lot. Everything is working just fine now.
--
Where there is a WISH, there is a WILL.