Click to See Complete Forum and Search --> : Pressing Enter in Dialog


June 3rd, 1999, 10:26 AM
I have a dialog that has multiple edit boxes on it. This dialog opens when a button is pressed. These edit boxes are set up to show at different times on a tab control. The problem I am having is when I press enter on any of the edit boxes the dialog closes. How do I stop the dialog from closing or hook the enter key.

Roger Allen
June 3rd, 1999, 11:04 AM
Your problem relates to the default button. When you press enter, MFC routes the enter key as a click on the default button (which is by default the OK button, ID of IDOK) and your dialog boxes OnOK() function gets called.
You can get around this by overriding the default OnOK() and make sure it doesn't call the CDialog::OnOK() function (which closes the dialog box). Then when you do need to close the dialog box, you can have your own OK button that calls the CDialog::OnOK() function as required.


Roger Allen

David Giovannini
June 3rd, 1999, 04:53 PM
I have a similar problem- maybe you can help.
I have a dialog with a default button. Return key does not call the OnOK. Is there another flag to set to enable that processing? The dialog is modal with a list box, password field, and OK, and Cancel buttons.

Dave

Roger Allen
June 4th, 1999, 05:46 AM
From what I remember, if you have a default button set, which is not the OK button, then pressing enter will cause windows to assume a press of that button. If you need to route it through the OK button, you will either have to make the OK button your default button, or you will have to override the PreTranslateMessage for you dialog box and trap the ENTER key to do the processing you require at the right time. Example code is:


BOOL CDialogClass::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN)
{
TRACE("%d %d\n", pMsg->wParam, pMsg->lParam) ;
switch (pMsg->wParam)
{
case VK_ENTER : // enter
// do you processing fro special occurance here
// return true if you dealt with the message
return true ;
}
}
return CDialog::PreTranslateMessage(pMsg);
}





HTH


Roger Allen