Hi,
How do I go about getting the character position along a given line wherever the caret is placed?
I can get the CaretPosition easily enough, but the I don't want the X & Y coordinates. I need to count how many characters there are along a line up to where the caret is placed.
Thanks for your time,
Rob
How do I go about getting the character position along a given line wherever the caret is placed?
I can get the CaretPosition easily enough, but the I don't want the X & Y coordinates. I need to count how many characters there are along a line up to where the caret is placed.
Thanks for your time,
Rob
6 Answers, 1 is accepted
0
Robert
Top achievements
Rank 1
answered on 21 May 2012, 05:13 PM
Ok I think I worked this out myself.
This seems to work well. It will count the number of characters along a line up to where the caret is positioned.
It will ignore any custom annotation tags that you may have and only count the number of characters in all of the found Spans on a given line.
Method Call:
Main method:
Here is the method code for "IsCaretAtLineStart":
I'm all ears (or eyes) if anyone has a better idea.
Thanks,
Rob
This seems to work well. It will count the number of characters along a line up to where the caret is positioned.
It will ignore any custom annotation tags that you may have and only count the number of characters in all of the found Spans on a given line.
Method Call:
DocumentPosition position =
new
DocumentPosition(radRichTextBox.Document.CaretPosition);
//Get the caret character count on the current line
int
charCount = GetCaretCharacterPositionAlongLine(position);
MessageBox.Show(
"Character Count: "
+ charCount.ToString());
Main method:
public
static
int
GetCaretCharacterPositionAlongLine(DocumentPosition position)
{
int
charCount = 0;
while
(IsCaretAtLineStart(position) ==
false
)
{
position.MoveToPrevious();
if
(position.GetCurrentInlineBox().AssociatedInline
is
Span)
{
charCount++;
}
}
return
charCount;
}
Here is the method code for "IsCaretAtLineStart":
public
static
bool
IsCaretAtLineStart(DocumentPosition position)
{
DocumentPosition startLinePosition =
new
DocumentPosition(position);
startLinePosition.MoveToCurrentLineStart();
if
(position == startLinePosition)
{
return
true
;
}
return
false
;
}
I'm all ears (or eyes) if anyone has a better idea.
Thanks,
Rob
0
Hello Robert,
so calling this method on every move can be improved if you move just once to the beginning, and then go to next element till it is the current position.
Don't hesitate to contact us if you have other questions.
Kind regards,
Martin
the Telerik team
We think your solution to the problem is good. Here is a little tip:
This is the implementation of MoveToCurrentLineStart():
public
bool
MoveToCurrentLineStart()
{
this
.CallOnPositionChanging();
InlineLayoutBox startInlineBox =
this
.GetCurrentInlineBox();
InlineLayoutBox currInlineBox = startInlineBox;
bool
hasPrevious =
true
;
while
(hasPrevious && currInlineBox.LineInfo == startInlineBox.LineInfo)
{
hasPrevious =
this
.MoveToPreviousInternal();
currInlineBox =
this
.GetCurrentInlineBox();
}
if
(currInlineBox ==
null
|| currInlineBox.LineInfo != startInlineBox.LineInfo)
{
this
.MoveToNextInternal();
}
this
.CallOnPositionChanged(
true
);
return
true
;
}
Don't hesitate to contact us if you have other questions.
Kind regards,
Martin
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
0
Robert
Top achievements
Rank 1
answered on 24 May 2012, 09:38 AM
Hi Martin,
Thanks for responding to this post. Your input is greatly appreciated.
I see from what you are saying is that I am making several MoveToLineStart calls that could actually be reduced, thus improving on the performance. Is that correct?
Rob
Thanks for responding to this post. Your input is greatly appreciated.
I see from what you are saying is that I am making several MoveToLineStart calls that could actually be reduced, thus improving on the performance. Is that correct?
Rob
0
Robert
Top achievements
Rank 1
answered on 24 May 2012, 11:29 AM
Sorry I posted this in the wrong thread.
0
Robert
Top achievements
Rank 1
answered on 24 May 2012, 11:55 AM
Sorry I posted this in the wrong thread.
0
Hello Robert,
Martin
the Telerik team
You have understood correctly, the reduced number of calls to MoveToLineStart will improve the performance. Of course, you should test your application and decide which is better for you.
All the best,Martin
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>