How to determine which 'line' the cursor is on?

7 posts, 0 answers
  1. Russell
    Russell avatar
    5 posts
    Member since:
    Apr 2009

    Posted 08 Apr 2011 Link to this post

    Hi All,

    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?



    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?
  2. Iva Toteva
    Iva Toteva avatar
    1319 posts

    Posted 14 Apr 2011 Link to this post

    Hi Russell,

    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)
            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 

    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();

    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Brian
    Brian avatar
    73 posts
    Member since:
    Sep 2010

    Posted 09 Jul 2012 Link to this post

    Is it possible to get all text up to given caret position?
  4. Iva Toteva
    Iva Toteva avatar
    1319 posts

    Posted 11 Jul 2012 Link to this post

    Hi Brian,

    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);
    string selectedText = this.editor.Document.Selection.GetSelectedText();
    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 >>

  5. Brian
    Brian avatar
    73 posts
    Member since:
    Sep 2010

    Posted 12 Jul 2012 Link to this post

    Using the Selection property does not work well.  It changes what is currently selected on the screen.  I have attempted to reset the selection on the screen back to its original state, but this has caused other issues.  Seems very odd that I can't simply get the text up to a certain point within the document, or even between points. 
    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);
    // save the text position where a new statement would be inserted
    CurrentlySelectedPFText = ctl.Document.Selection.GetSelectedText();
    // clear the text that was selected
    // reselect any selected text
    if (selectedStartPosition != null || selectedLastPosition != null)
    // reset the cursor position

    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.
  6. Petya
    Petya avatar
    989 posts

    Posted 17 Jul 2012 Link to this post

    Hello Brian,

    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);
    DocumentSelection selection = new DocumentSelection(radDocument);
    string text = selection.GetSelectedText();

    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  7. arab
    arab avatar
    2 posts
    Member since:
    Nov 2016

    Posted 22 Nov 2016 in reply to Russell Link to this post


    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?


Back to Top