Click here to Skip to main content
Licence 
First Posted 9 Aug 2002
Views 65,631
Bookmarked 16 times

Quick positioning of the caret at the end of the text

By | 9 Aug 2002 | Article
Quickly position the caret at the end of edit control text...

Problem

I was using EditControl window to dump some logging/trace messages line by line and found out that a simple routine involving:

SendDlgItemMessage(hwndDlg,IDC_SOMEEDITCONTROL, 
    EM_REPLACESEL,0,(long)lpszTextBuffer);

works only if you don't reposition the caret in EditControl window (which inadvertantly happens when you scroll back and forth to check on previous messages...). So, if you by accident repositioned the caret, the next text dump will disrupt existing line and produce undesirable effect of garbled data...

A Solution

A simple workaraound may help -- the following "duo" does the trick:

SendDlgItemMessage(hwndDlg,IDC_SOMEEDITCONTROL, EM_SETSEL, 0, -1);
SendDlgItemMessage(hwndDlg,IDC_SOMEEDITCONTROL, EM_SETSEL, -1, -1);

This will position a runaway caret to the end of the text in EditControl..., so that the next call to:

SendDlgItemMessage(hwndDlg,IDC_SOMEEDITCONTROL, 
    EM_REPLACESEL,0,(long)lpszTextBuffer);

will append a new line to the bottom of the existing body of text... ;-)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Botticelli

Web Developer

United States United States

Member

Programming "Artist" since 1984

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralCaret at the beginning PinmemberFilomela3:37 28 Apr '04  
GeneralRe: Caret at the beginning Pinmembertoxcct23:46 4 Aug '05  
QuestionHow to highlight a particular line in edit box? PinmemberRajksingh3:46 17 Sep '03  
AnswerMFC Solution PinmemberJohn A. Johnson11:01 31 Oct '03  
AnswerSDK Solution PinmemberJohn A. Johnson11:10 31 Oct '03  
AnswerAuto scroll Pinmembera.bietti5:34 8 Dec '04  
GeneralRe: Auto scroll Pinmember42885:33 19 Jul '07  
GeneralAll Documented in MSDN PinmemberNorm Almond8:16 10 Aug '02  
GeneralRe: All Documented in MSDN Pinmemberbob169726:20 8 Sep '03  
GeneralRe: All Documented in MSDN PinmemberNormski7:52 10 Sep '03  
GeneralRe: All Documented in MSDN Pinmemberbob1697216:05 23 Jan '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 10 Aug 2002
Article Copyright 2002 by Botticelli
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid