Thread: Hitting enter while FOCUS on an EDIT

  1. #1
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584

    Hitting enter while FOCUS on an EDIT

    Okay, I have a single line edit. And when the user types something, for example "bunny rabbit" and then they hit ENTER to enter it, how would I be able to capture this ENTER from the edit box to handle it?

    I have tried WM_CHAR, but I guess that is only for focus on the main window.

    Any ideas?

    Thanks!
    1978 Silver Anniversary Corvette

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    The simplest way is to give your edit contol the ES_MULTILINE | ES_WANTRETURN styles. If that's no good for you then subclass the edit and handle, for example, WM_KEYDOWN for the control.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    But if I have this ES_WANTRETURN, how would that work? How will I know when ENTER is hit?
    1978 Silver Anniversary Corvette

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Had to test it to find the exact message but here you go:

    On a single line edit box with ES_MULTILINE and ES_WANTRETURN when the user hits ENTER a WM_COMMAND message will be generated, the LOWORD of wParam will be the HMENU and the HIWORD will be EN_MAXTEXT.

    So just process the EN_MAXTEXT message. Becareful because the message could be generated by the user filling the messagebox also

  5. #5
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Strangely, a very similar question was given on Nerdbrains a few days ago. It used subclassing to solve the problem and the thread contains an example. The original thread can be viewed here.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    If in doubt RTM

    "ES_WANTRETURN Specifies that a carriage return be inserted when the user presses the ENTER key while entering text into a multiple-line edit control in a dialog box. Without this style, pressing the ENTER key has the same effect as pressing the dialog box’s default pushbutton. This style has no effect on a single-line edit control. "
    MSDN
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I posted a subclassing solution for single line edit boxes in this thread.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Originally posted by novacain
    If in doubt RTM

    "ES_WANTRETURN Specifies that a carriage return be inserted when the user presses the ENTER key while entering text into a multiple-line edit control in a dialog box. Without this style, pressing the ENTER key has the same effect as pressing the dialog box’s default pushbutton. This style has no effect on a single-line edit control. "
    MSDN
    Good lord, what was I thinking? Or rather, why wasn't I thinking? Thanks for the timely intervention, Novacain, and apologies to Garfield for that (ie ES_WANTRETURN) piece of nonsense.

    edit: Although, in all fairness, I did mention subclassing.
    Last edited by Ken Fitlike; 09-02-2003 at 10:44 AM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Actually as I posted, if you use ES_WANTRETURN and ES_MULTILINE with an edit box that only has space for one line (maybe thats the point some aren't getting) hutting return will give an ES_MAXTEXT message that you can then process. I'm sure adrianxw's way is most likely better, but for those of us that haven't dealt with subclassing there is another way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Close console without hitting enter
    By Cathalo in forum C++ Programming
    Replies: 5
    Last Post: 05-02-2009, 08:32 AM
  2. Catching tab or enter in an EDIT control in a regular window
    By Boksha in forum Windows Programming
    Replies: 0
    Last Post: 11-19-2005, 10:27 AM
  3. Multiline Edit Box Parser
    By The Brain in forum Windows Programming
    Replies: 6
    Last Post: 11-01-2005, 07:15 PM
  4. Replies: 3
    Last Post: 07-23-2005, 08:00 AM
  5. Capturing Enter Key in Edit Boxs!!
    By poopy_pants2 in forum Windows Programming
    Replies: 6
    Last Post: 12-22-2002, 02:38 PM