1

A very straightforward question....

How do you enter a new line in a CEdit control box without it triggering OK and closing the dialog box altogether? What I mean is when you hit the enter key it automatically selects OK, even if your cursor is still in the CEdit control. Is what I am trying to do possible? Or do i have to use some other control

PS: It is a modal dialog box btw.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
user3396028
  • 37
  • 1
  • 9
  • 4
    In the dialog editor, set the "Multiline" and "Want Return" properties to True and it is all automagic from there. – Hans Passant Mar 24 '14 at 15:50

2 Answers2

3

There are various solutions for this problem.

Basically what you need is the ES_WANTRETURN style on the edit control.

Another solution is to check the message and key in PreTranslateMessage (since it has been commented upon this is not the recommended way, I'm just mentioning it for possibilities):

BOOL CYouDialog::PreTranslateMessage(MSG* pMsg)
{
    if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN &&
        GetFocus() == youcontrol)
    {
        return TRUE;
    }

    return FALSE;
}

The other solution is to handle WM_GETDLGCODE. You should subclass the edit control and do this:

UINT CYourEdit::OnGetDlgCode()
{
    return CEdit::OnGetDlgCode() | DLGC_WANTALLKEYS;
}

UPDATE: FYI, also have a look at Just because you're a control doesn't mean that you're necessarily inside a dialog box.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
  • After your days at CodeGuru, I'm surprised that you would recommend the [PreTranslateMessage hack](http://forums.codeguru.com/showthread.php?231075-MFC-Dialog-How-to-disable-change-the-behaviour-of-the-lt-gt-key-in-a-dialog) – rrirower Mar 24 '14 at 15:59
-1

The default dialog processing, as you've discovered, is to close a dialog box when enter is pressed. MFC actually executes the OnOK processing, but you can override that. Here's and old explanation, but, it's still relevant.

rrirower
  • 4,338
  • 4
  • 27
  • 45