Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Quick positioning of the caret at the end of the text

0.00/5 (No votes)
9 Aug 2002 1  
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