Win32/C++. I have a multiline edit control and a pushbutton that I've made default with DM_SETDEFID
. When I hit enter with focus on the edit, I want the focus to stay there instead of moving to the pushbutton.
Asked
Active
Viewed 661 times
2

user1299863
- 21
- 1
1 Answers
2
The edit control should have ES_WANTRETURN
style to change behavior to desired:
Specifies that a carriage return be inserted when the user presses the ENTER key while entering text into a multiline edit control in a dialog box. If you do not specify this style, pressing the ENTER key has the same effect as pressing the dialog box's default push button. This style has no effect on a single-line edit control.
To change this style after the control has been created, use SetWindowLong.

Roman R.
- 68,205
- 6
- 94
- 158
-
I should have been more clear. I do want the enter key to trigger the default button, but I want focus to stay with the edit control (and not insert a line break). – user1299863 Mar 29 '12 at 18:01
-
Then you don't need `ES_WANTRETURN` style, or otherwise it would have been eaten by then control, right? You pass it through and let the defaults transfer focus and trigger the button. Now you have `BN_CLICKED` notification called and you handle it. Here is your chance to do your job handling the notification, and then complete it by transferign focus to your edit control with `SetFocus`. – Roman R. Mar 29 '12 at 19:03
-
Things are getting trickier if you have several edit controls and you want to return focus to the one which had it before button. Then you might want to listen to `EN_KILLFOCUS` notifications and remember the last one before button click. Or, you can subclass button control and handle its `WM_SETFOCUS` to see what control focus travelled from to the button. Or, there might be additional dialog message useful for the purpose you can see using Spy++ tool and take advantage of. – Roman R. Mar 29 '12 at 19:06
-
Thanks. Spy++ revealed that the edit was returning `DLGC_HASSETSEL | DLGC_WANTARROWS | DLGC_WANTCHARS | DLGC_WANTALLKEYS` in response to the `WM_GETDLGCODE` message. After some experimenting I discovered that subclassing the edit and returning `DLGC_HASSETSEL | DLGC_WANTARROWS | DLGC_WANTCHARS` instead stops the button taking focus in the first place. – user1299863 Mar 29 '12 at 20:30