Click here to Skip to main content
Click here to Skip to main content

Quick positioning of the caret at the end of the text

By , 9 Aug 2002
 

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
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.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralCaret at the beginningmemberFilomela28-Apr-04 3:37 
Your article is great.It has helped me a lot because I needed to put the caret at the end of the text and I did not know how.Thanks a lot.Now I want to insert the name of my document in the document( of course first it must be saved).So, I have to insert it at the beggining of my document.Do you have any idea?
GeneralRe: Caret at the beginningmembertoxcct4-Aug-05 23:46 
((CEdit*)GetDlgItem(IDC_MY_EDIT))->SetSel(0, 0);

 

TOXCCT >>> GEII power
[toxcct][VisualCalc]
QuestionHow to highlight a particular line in edit box?memberRajksingh17-Sep-03 3:46 
Confused | :confused: I want to highlight a particular line in edit box, and auto scroll. how to do that?
 
R.K.Singh
AnswerMFC SolutionmemberJohn A. Johnson31-Oct-03 11:01 
Lifetime answer, but why not... if you are using MFC, you can use the...
 
CEDit::LineIndex( int nLine = -1 )
 
...call of a CEDit to retrieve the character position of the line you want to select, this means that you can do something like:
 
bool SelectLine(CEdit* pEdit, int nLine)
{
    int nStartPos = pEdit->LineIndex(nLine);
 
    if (nLine < 0)
        return false;
 
    int nEndPos = pEdit->LineIndex(nLine+1);
 
    pEdit->SetSel(nStartPos, nEndPos);
 
    return true;
}
 
Cool | :cool:
AnswerSDK SolutionmemberJohn A. Johnson31-Oct-03 11:10 
... and to maintain the article style:
 
bool SelectLine(HWND hWndDlg, UINT uCtrl, int nLine)
{
    int nStartPos =
        SendDlgItemMessage(
            hwndDlg,
            uCtrl, EM_LINEINDEX, nLine, 0); 
 
    if (nLine < 0)
        return false;
 
    int nEndPos =
        SendDlgItemMessage(
            hwndDlg,
            uCtrl, EM_LINEINDEX, nLine+1, 0); 
 
    SendDlgItemMessage(
        hwndDlg,
        uCtrl, EM_SETSEL, nStartPos, nEndPos); 
 
    return true;
}
 
Cool | :cool:
AnswerAuto scrollmembera.bietti8-Dec-04 5:34 
Deceived by the innefective EM_SCROLLCARET (!), I finally found this trick:
 
LRESULT result = 0;
do
{
result = SendMessage(hwnd, EM_SCROLL, SB_PAGEDOWN, 0);
}
while ((HIWORD(result) == TRUE) && (LOWORD(result) > 0));
GeneralRe: Auto scrollmember428819-Jul-07 5:33 
Keep this solution around man, this is the only method i've found to scroll down the caret in a edit box in a win32 environment (without mfc)
 
---

GeneralAll Documented in MSDNmemberNorm Almond10-Aug-02 8:16 
back in '92 when MSDN was first published on CD Unsure | :~
 
Normski. - the next bit of code is self modifying ... jmp 0xCODE
GeneralRe: All Documented in MSDNmemberbob169728-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 MSDNmemberNormski10-Sep-03 7:52 
Sorry with over 12years of Win16/32 programming experience I just take for granted the first place to look for stuff like this is either the good ole' Charles Petzold Programming Windows, for at least look at MSDN.
 
JFYI (Just for your interest) the orginal Windows programming manuals came in a box of about 22 books.
GeneralRe: All Documented in MSDNmemberbob1697223-Jan-04 16:05 
I've only got 15 years experience in programming but I would be lost without MSDN or this site. I usually check MSDN first, then search engines, then my books. Finding this snippet saved me a bunch of time.
 
I'm not sure what your comment about the 22 books thingy was or what it has to do with my comment but your welcome to them. There's nothing better than having a question, typing it in, and getting 5 million-zillion links to helpful comments and tidbits like this one. And, by the way, some of your posts have helped me from time to time so thank you for posting them.
 
I promise I won't criticize you if I find stuff from your articles somewhere in the deep dark halls of MSDN or in a Petzold book. I'd rather find too many links to helpful pages than "No search results were found"

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

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