Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Caret Position

Posted on 1998-05-15
13
Medium Priority
?
450 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Author Comment

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

Accepted Solution

by:
RONSLOW earned 150 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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.

636 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