Caret Position

I am developing an editor and I need to set the Caret Position to that many tab stops as the previous line when I hit the enter key to type a new line of text.
I have my function calculate the number of tab stops (and the corresponding no of character and coordinate positions) before I begin typing a line of text. Everytime I type a character, my handler does a GetCaretPos() to get the current position and store it in a CPoint variable. When I hit the Enter Key, I have my handler add the no. of coordinate positions(which is the no. of tab characters in the prev. line) to the 'x' member of the CPoint variable to get the new position and then I call SetCaretPos(CPoint variable). But it fails to set the position.

Any help would be much appreciated
LVL 2
kavintAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kavintAuthor Commented:
Edited text of question
0
kavintAuthor Commented:
Edited text of question
0
kavintAuthor Commented:
Edited text of question
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

kavintAuthor Commented:
Edited text of question
0
RONSLOWCommented:
Use SetSel to set the selection position (after ensuring the approriate whitespace is present).  SetSel will also set the cursor (caret) position approriately (unless you tell it not to).

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kavintAuthor Commented:
Thanks much for yr suggestion.
I did try the SetSel option before and I tried it after your suggestion but it doesnt seem to set the position.
One more thing, should I add any piece of code to make sure that my tab key is recognized as 8 characters and not as a single character as it seems to take it now. GetSel increments the value by just one when I hit a Tab key. Will SetTabStops help?
Thanks you.
0
RONSLOWCommented:
BTW I assume then that you are using a rich text edit control.

SetTabStops will do the trick, however, this is not controlled in character units, I _think_ it is in twips.  In that case you need to get the font metrics to work out the average size of a character and then convert that to twips (or whatever) for use in SetTabStops.

BTW2: Are you sure that you insert an approirate number of spaces .. I don't think the caret/selection won't move to a position if there is nothing there.

BTW3: check out www.codeguru.com for richtextedit (and other) articles .. they may give you some ideas and/or code that will help

0
kavintAuthor Commented:
Your guess is right. I havent inserted the right no. of spaces!
But I dont have an idea as to how to insert them ..

Please help
Thanks
0
kavintAuthor Commented:
And I am using a CEditView
0
RONSLOWCommented:
Get the test of the edit control .. and append the end of line (\n\r) and then an appropriate number of spaces (probably the same number as at the start of the previous non-blank line).

You'll probably want to trim any lines before you finish that just consist of blanks only.

0
kavintAuthor Commented:
I did try that and other options too. As you've said, I did append a \n\r and then the appropriate no. of spaces. When I stepped thru the code, my string seems to show the right value(with the line feed and the no. of spaces)But when I do a ReplaceSel of the same string, it does not interpret the carriage return and line feed as a new line. Instead it displays some junk characters at the end of the line

0
RONSLOWCommented:
Hmmm .. can you post the code tha you are using please?

0
kavintAuthor Commented:
Here it is


nLineCount = GetEditCtrl().GetLineCount();
GetEditCtrl().GetLine(nLineCount-1,sLine); /* Get the text of the previous line */
nLength = strlen(sLine);
m_nIndex = nLength;

sLine[m_nIndex++] = '\n';
sLine[m_nIndex++] = '\r';

for(i=0;i < m_nTabCount;i++)
     sLine[m_nIndex++] = ' '; /* Append as many blank spaces */
sLine[m_nIndex] = '\0';

if(m_nTabCount > 0) /* If I need to insert spaces for my new line */
{
     GetEditCtrl().SetSel(m_nEnterKeyPosition-nLength,m_nEnterKeyPosition);
     GetEditCtrl().ReplaceSel(sLine);
}
Note :- I have not included the computation of the m_nEnterKeyPosition since I felt it was not relevant for this problem.
Thanks again
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.