Thread: Highlighting text in an EditBox

  1. #1
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591

    Highlighting text in an EditBox

    I am trying to set my editbox up so that when the user clicks on an edit box, the text inside the editbox will automatically become highlighted. I figured sending an EM_SETSEL message on every EN_SETFOCUS notification would do that easily, except for the little snag that it isn't working: the caret position scrolls to the right a bit, but thats about it... no text is highlighted at all. Here is my simple code:
    Code:
    case EN_SETFOCUS:
    {
         SendMessage((HWND)lParam, EM_SETSEL, (WPARAM)0, (LPARAM)-1);
         break;
    }
    According to MSDN, a wparam of 0 and an lparam of -1 should select all text, but it does not do this (nor for any combination of start/end values I have tried).
    There's probably some simple thing I'm missing, but other than that I have no clue as to why something so simple has become so hard, please help, thanks.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Is your EN_SETFOCUS ever fired? You do understand that it's sent as notification to the edit control's parent as a WM_COMMAND message and not as a discrete message?
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Yes, that was just a small snippet cut out from the larger WM_COMMAND message catch.
    Here is the more complete form:
    Code:
      case WM_COMMAND:
           switch(LOWORD(wParam))
           {
               case IDC_NAMEEDIT:
                   switch(HIWORD(wParam))
                   {
                       case EN_SETFOCUS:
                           SendMessage((HWND)lParam, EM_SETSEL, (WPARAM)0, (LPARAM)-1);
                           break;
                       default:
                           return FALSE;
                   }
                   break;
         ....
    I know for sure it's firing because I tested it by putting a messageBox in the notification, which pops up as expected every time I focus the edit box. As I mentioned, it makes some signs of an attempt to highlight (the caret scrolls right) but no actual text is highlighted, what is going wrong here?

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by @nthony
    the caret position scrolls to the right a bit, but thats about it
    Not a useful diagnostic, given:
    Quote Originally Posted by EM_SETSEL
    Edit controls: The control displays a flashing caret at the end position regardless of the relative values of start and end.
    When an edit control gains the focus as the result of a mouse click, selection of all the text in it occurs only if that control is not a single line edit, when it's sent an EM_SETSEL message.

    Use PostMessage instead.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Hey thanks! That works perfectly now!
    I was wondering what exactly that blurb in the remarks section meant (I thought it odd it might negate the effects of its own selection in an EM_SETSEL message). I'm curious though, PostMessage differs from SendMessage in that it returns immediately, but what is it about the EM_SETSEL message that requires it to be posted to the message queue instead of being sent and waiting for it's processing? (I guess what I'm asking is, under what conditions/messages should PostMessage be used versus SendMessage?)
    Last edited by @nthony; 01-17-2007 at 05:52 PM.

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    This is my guess to what is happening.

    According to the docs:
    ES_NOHIDESEL
    Negates the default behavior for an edit control. The default behavior hides the selection when the control loses the input focus and inverts the selection when the control receives the input focus. If you specify ES_NOHIDESEL, the selected text is inverted, even if the control does not have the focus.
    So I speculate this is what happens when you send the EM_SETSEL:
    1. The edit control gains focus.
    2. EN_SETFOCUS is called.
    3. You send the EM_SETSEL message which is processed.
    4. The control restores the selection from before it lost focus, thus reversing your EM_SETSEL.

    When you post a message, it is added to the message queue rather than being called immediately, so the sequence of events is now:
    1. The edit control gains focus.
    2. EN_SETFOCUS is called.
    3. You post the EM_SETSEL message which waits in the message queue.
    4. The control restores the selection from before it lost focus.
    5. The EM_SETSEL message in the queue is processed.

    Incidentally, I thought of this when I first read your post but couldn't think of a neat solution, so nice work Ken for suggesting PostMessage.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 12:23 AM
  2. Transparent, Editbox, HOLLOW_BRUSH, Marking text?
    By Dampy in forum Windows Programming
    Replies: 6
    Last Post: 09-22-2008, 07:17 PM
  3. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  4. Appending text to an edit control
    By dit6a9 in forum Windows Programming
    Replies: 3
    Last Post: 08-13-2004, 09:52 PM
  5. Replies: 1
    Last Post: 07-13-2002, 05:45 PM