Click here to Skip to main content
16,016,814 members
Articles / Desktop Programming / MFC
Article

Quick positioning of the caret at the end of the text

Rate me:
Please Sign up or sign in to vote.
4.75/5 (11 votes)
9 Aug 2002 85.1K   16   11
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


Written By
Web Developer
United States United States
Programming "Artist" since 1984

Comments and Discussions

 
GeneralCaret at the beginning Pin
Filomela28-Apr-04 3:37
Filomela28-Apr-04 3:37 
GeneralRe: Caret at the beginning Pin
toxcct4-Aug-05 23:46
toxcct4-Aug-05 23:46 
QuestionHow to highlight a particular line in edit box? Pin
gurumj17-Sep-03 3:46
gurumj17-Sep-03 3:46 
AnswerMFC Solution Pin
andyj11531-Oct-03 11:01
andyj11531-Oct-03 11:01 
AnswerSDK Solution Pin
andyj11531-Oct-03 11:10
andyj11531-Oct-03 11:10 
AnswerAuto scroll Pin
a.bietti8-Dec-04 5:34
a.bietti8-Dec-04 5:34 
GeneralRe: Auto scroll Pin
428819-Jul-07 5:33
428819-Jul-07 5:33 
GeneralAll Documented in MSDN Pin
NormDroid10-Aug-02 8:16
professionalNormDroid10-Aug-02 8:16 
GeneralRe: All Documented in MSDN Pin
bob169728-Sep-03 6:20
bob169728-Sep-03 6:20 
With all due respect, I'm not saying it's not there, but it did not jump in my lap when I looked for it in MSDN. It may have been at one time, or it may be in one of the various samples or one of the deep dark corners of the MSDN (which I think is outstanding by the way) or it may have even been right under my nose and I failed to see it and passed it by (pretty common occurance with me).

He spent some time trying to find this and thought he'd share it online just in case someone else had trouble finding it. It's a very useful tip and probably in every book by now but it a very useful and probably sought after trick for a beginner. This site is filled with redundant stuff but I would caution you not to criticize every user who posts something that may already exist in the MSDN.

I have found a great deal of good advise on this site and would still be looking through my books and MSDN for some of them if they weren't posted. Just because it's there, doesn't mean everyone will find it...quickly. Let's not get so high on ourselves that we forget this site is about learning...not bragging about what we already know.

Too many people bash others on this site and it's really sad that people don't realize that we'd all be "nowhere" without documentation (and lot's of it).
GeneralRe: All Documented in MSDN Pin
NormDroid10-Sep-03 7:52
professionalNormDroid10-Sep-03 7:52 
GeneralRe: All Documented in MSDN Pin
bob1697223-Jan-04 16:05
bob1697223-Jan-04 16:05 

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

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