I have a RadRichTextBox, into which the user cuts and pastes the contents of a text file. I'm using the TxtDataProvider with the RadRichTextBox, and I'm curious about one thing:
As the user moves the mouse up or down in the RadRichTextBox, is there any way that I can determine which "line" in the document that the cursor is currently on? I'm not wrapping the text or anything, so each visual line in the editor corresponds to an actual line in the text data that was pasted into the control.
As the cursor moves up or down, I'd just like to be able to determine which line in the text data corresponds to the document line that the caret is currently on. Is there a way to do this?
Thanks,
Russ
PS: I tried catching the command execution event and looking at the caret commands - that seems to be a start, but the "position" that I can retrieve from the document is a pair of floating point coordinates. Am I on the right track here - do I need to calculate the actual height of a line and then just divide that Y coordinate by the line height? If so, how do you calculate the line height(s) correctly?
6 Answers, 1 is accepted
You can get the line number of a DocumentPosition in the following way:
private
int
GetLineNumberFromDocumentPosition(DocumentPosition documentPosition)
{
InlineLayoutBox inline = documentPosition.GetCurrentInlineBox();
Paragraph parent = inline.Parent.AssociatedDocumentElement
as
Paragraph;
int
count = 0;
foreach
(Paragraph paragraph
in
this
.rrtb.Document.EnumerateChildrenOfType<Paragraph>())
{
if
(paragraph == parent)
{
break
;
}
count += (paragraph.Inlines.Last.GetAssociatedLayoutBoxes().Last()
as
InlineLayoutBox).LineInfo.IndexInParagraph + 1;
//+1 because counting starts at 0
}
count += documentPosition.GetCurrentInlineBox().LineInfo.IndexInParagraph;
return
count;
}
If you wish to get the number of the line that the caret is in, you need to invoke this method with the argument
this
.rrtb.Document.CaretPosition
In case you wish to get the line that the cursor is in (on MouseMove for instance), you can do that like this:
private
void
rrtb_MouseMove(
object
sender, MouseEventArgs e)
{
Point position = e.GetPosition(
this
.rrtb);
DocumentPosition documentPosition =
this
.rrtb.ActiveEditorPresenter.GetDocumentPositionFromViewPoint(position);
this
.tb.Text = GetLineNumberFromDocumentPosition(documentPosition).ToString();
}
Iva
the Telerik team

You can get all the text or document elements between two positions using selection. More information on document positions and selection can be found in the Positioning article and the Selection article.
In your case, you should create one position for the start of the document and use it as selection start. The selection end position would be the current caret position:
DocumentPosition beginningOfDocument =
new
DocumentPosition(
this
.editor.Document);
this
.editor.Document.Selection.SetSelectionStart(beginningOfDocument);
this
.editor.Document.Selection.AddSelectionEnd(
this
.editor.Document.CaretPosition);
string
selectedText =
this
.editor.Document.Selection.GetSelectedText();
//or
DocumentFragment selectedDocumentContentWithFormatting =
this
.editor.Document.Selection.CopySelectedDocumentElements();
All the best,
Iva Toteva
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

My issue is that I need to know what is BEFORE the cursor at all times. So, I run the following code on all mouse left button up events and all key up events:
// save the current cursor position and any highlighted text
Point cursorPosition = ctl.Document.CaretPosition.Location.ToPoint();
var selectedStartPosition = ctl.Document.Selection.Ranges.First;
var selectedLastPosition = ctl.Document.Selection.Ranges.Last;
DocumentPosition startPosition = ctl.Document.CaretPosition;
DocumentPosition endPosition = new DocumentPosition(startPosition);
startPosition.MoveToFirstPositionInDocument();
ctl.Document.Selection.AddSelectionStart(startPosition);
ctl.Document.Selection.AddSelectionEnd(endPosition);
// save the text position where a new statement would be inserted
CurrentlySelectedPFText = ctl.Document.Selection.GetSelectedText();
// clear the text that was selected
ctl.Document.Selection.Clear();
// reselect any selected text
if (selectedStartPosition != null || selectedLastPosition != null)
{
ctl.Document.Selection.AddSelectionStart(selectedStartPosition.StartPosition);
ctl.Document.Selection.AddSelectionEnd(selectedLastPosition.EndPosition);
}
// reset the cursor position
ctl.Document.CaretPosition.SetPosition(cursorPosition.ToPointF());
This works somewhat correctly but I find the cursor tends to move up one line when you are just typing something into the control. Any ideas how I can better get all text BEFORE the cursor or a better way to implement this would be greatly appreciated.
When you use the Selection property of your RadRichTextBox's Document, it changes the selection shown on the screen. Instead, you can create a new DocumentSelection instance. Then you can select the text like this:
DocumentPosition startPosition = radRichTextBox.Document.CaretPosition;
DocumentPosition endPosition =
new
DocumentPosition(startPosition);
startPosition.MoveToFirstPositionInDocument();
DocumentSelection selection =
new
DocumentSelection(radDocument);
selection.SetSelectionStart(startPosition);
selection.AddSelectionEnd(endPosition);
string
text = selection.GetSelectedText();
Petya
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Hello,
I need help, i have radrichtextbox, i want te get the content of selected text, i used mousepoint pour récupérer le point de selection puis je convert en documentposition pour pouvoir récupérer le mot :
Point mousePoint = e.GetPosition(this.Editor);
DocumentPosition position = this.Editor.ActiveEditorPresenter.GetDocumentPositionFromViewPoint(mousePoint);
string word = position.GetPreviousSpanBox().Text;
Ce code me rècupère le dernier mot du texte selectionné alors que je veux récupérer la totalité du texte selectionné.
SVP j'ai besoin de votre aide, qu'est ce que je dois faire?