Setting text cursor position inside edit

This is a discussion on Setting text cursor position inside edit within the Windows Programming forums, part of the Platform Specific Boards category; Is it possible and, if so, how is it done? At the startup of my program I create an edit ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    38

    Setting text cursor position inside edit

    Is it possible and, if so, how is it done?

    At the startup of my program I create an edit and then fill it with some text. I then set the focus to the edit so that the text cursor will appear there. The problem is that the cursor is at the beginning of the text string and not at the end (where I want it to be). I haven't been able to find anything on msdn or by searching here.

    ________
    Rutabega

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    uhhhh, something about the caret....lemme look quick

    *browses msdn*

    ok, here's a combination of code that should be helpful, some taken from OST2, some taken from msdn:

    k, so we've got a message that we can use for edit boxes (found from msdn here) called EM_SETSEL meaning Edit message set selection or something like that. Anyways, here's the parameters we need to send to it:

    Code:
       lResult = SendMessage(
       		// returns LRESULT in lResult
          (HWND) hWndControl,
       		// handle to destination control
          (UINT) EM_SETSEL,
       		// message ID
          (WPARAM) wParam,
       		// = (WPARAM) () wParam;
          (LPARAM) lParam
       		// = (LPARAM) () lParam;
       );
    bla blah blah

    Code:
     wParam   Specifies the starting character position of the selection.
      lParam Specifies the ending character position of the selection.
    Ok, so we need to know the start and end position. You said you want the selection to be at the end? then we need to find the length of the edit box:

    use this function/code
    Code:
       DWORD TextSize;
       TextSize=GetWindowTextLength(EditBox);
    Where EditBox is an HWND to your editbox, duh.

    So now we have the text size....here we go!

    Code:
    SendMessage(EdtiBox,EM_SETSEL,TextSize,TextSize);
    and tadah! We've got the position set. You might also need to dabble with the caret (not carrot) position if you have a mutltiline edit control:

    This is easy to do, just use this message, like so:

    Code:
    SendMessage(EditoBox,EM_SCROLLCARET,0,0);
    and voila, it scrolls our caret (the little flashing line thingy) in to view automatically!

    -hope that helps!
    Last edited by jverkoey; 07-15-2004 at 05:03 AM. Reason: stupid WSIWYG editor lied to me....like usual

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    38
    Thanks man!!

    That worked perfectly.

    I was kind of amused by this note in msdn for EM_SCROLLCARET

    " Return Value
    The return value is not meaningful. "

    Thanks again
    ________
    Rutabega

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struggling to read text from Edit Control (box)
    By csonx_p in forum Windows Programming
    Replies: 6
    Last Post: 05-13-2008, 05:55 AM
  2. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Getting my text from edit box
    By Marky_Mark in forum Windows Programming
    Replies: 2
    Last Post: 11-16-2001, 01:06 PM
  5. adding text to an edit control
    By steve_i83 in forum Windows Programming
    Replies: 2
    Last Post: 10-26-2001, 07:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21