Solved

Caret Position

Posted on 1998-05-15
13
436 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now