Solved

Caret Position

Posted on 1998-05-15
13
439 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:kavint
  • 9
  • 4
13 Comments
 
LVL 2

Author Comment

by:kavint
ID: 1177907
Edited text of question
0
 
LVL 2

Author Comment

by:kavint
ID: 1177908
Edited text of question
0
 
LVL 2

Author Comment

by:kavint
ID: 1177909
Edited text of question
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 2

Author Comment

by:kavint
ID: 1177910
Edited text of question
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 50 total points
ID: 1177911
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
 
LVL 2

Author Comment

by:kavint
ID: 1177912
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1177913
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
 
LVL 2

Author Comment

by:kavint
ID: 1177914
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
 
LVL 2

Author Comment

by:kavint
ID: 1177915
And I am using a CEditView
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1177916
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
 
LVL 2

Author Comment

by:kavint
ID: 1177917
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1177918
Hmmm .. can you post the code tha you are using please?

0
 
LVL 2

Author Comment

by:kavint
ID: 1177919
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

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question