This is a migrated thread and some comments may be shown as answers.

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

6 Answers 527 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Russell
Top achievements
Rank 1
Russell asked on 08 Apr 2011, 07:51 PM
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?

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

Sort by
0
Iva Toteva
Telerik team
answered on 14 Apr 2011, 09:48 AM
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)
        {
            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();
}

Regards,
Iva
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
0
Brian
Top achievements
Rank 1
answered on 09 Jul 2012, 06:35 PM
Is it possible to get all text up to given caret position?
0
Iva Toteva
Telerik team
answered on 11 Jul 2012, 05:36 PM
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);
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 >>

0
Brian
Top achievements
Rank 1
answered on 12 Jul 2012, 08:17 PM
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);
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.
0
Petya
Telerik team
answered on 17 Jul 2012, 03:37 PM
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);
startPosition.MoveToFirstPositionInDocument();
 
DocumentSelection selection = new DocumentSelection(radDocument);
selection.SetSelectionStart(startPosition);
selection.AddSelectionEnd(endPosition);
 
string text = selection.GetSelectedText();

Greetings,
Petya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
arab
Top achievements
Rank 1
answered on 22 Nov 2016, 04:57 PM

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?

 

Tags
RichTextBox
Asked by
Russell
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Brian
Top achievements
Rank 1
Petya
Telerik team
arab
Top achievements
Rank 1
Share this question
or