Click to See Complete Forum and Search --> : CEdit and Return Key


Djibril
October 19th, 1999, 02:11 PM
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.

ChrisD
October 19th, 1999, 03:04 PM
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

ChrisD
October 19th, 1999, 03:06 PM
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

Djibril
October 19th, 1999, 03:31 PM
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.

rabid_toaster
October 19th, 1999, 03:57 PM
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);

Djibril
October 19th, 1999, 04:05 PM
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.