How do I prevent multi-line edit controls from eating the Enter key?

Raymond Chen

You might decide to put a multi-line edit control in a dialog box, not because you want the user to input multi-line data, but because it’s a convenient way to display multi-line text. When you do that, you may notice that the Enter key does not invoke the default dialog button, as you might normally expect. That’s because the multi-line edit control tells the dialog manager that it wants the Enter key. So how do you tell the multi-line edit control to stop doing that and let the Enter key do its normal thing? You already know the answer to this. As we saw quite some time ago, a control responds to the WM_GETDLGCODE message to influence the behavior of the dialog manager. In this case, the edit control is returning DLGC_WANTMESSAGE in response to the keyboard Enter key. What you want to do is prevent this from happening. You want to change the value that the edit control returns to the WM_GETDLGCODE.

Since there is no existing window style to specify this behavior, you’re left with subclassing the control and removing the DLGC_WANTMESSAGE code if the message is the Enter key.


Discussion is closed.

Feedback usabilla icon