Solved

Caret Position

Posted on 1998-05-15
13
445 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

724 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