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

How to get line number

6 Answers 328 Views
RichTextBox (obsolete as of Q3 2014 SP1)
This is a migrated thread and some comments may be shown as answers.
Tommaso
Top achievements
Rank 1
Tommaso asked on 08 Oct 2012, 08:30 PM
I'm using the following snippet to find text

private void HightlightText(string textToHighlight)
{
    DocumentTextSearch search = new DocumentTextSearch(this.radRichTextBox1.Document);
    foreach (var textRange in search.FindAll(textToHighlight))
    {
        this.radRichTextBox1.Document.Selection.AddSelectionStart(textRange.StartPosition);
        this.radRichTextBox1.Document.Selection.AddSelectionEnd(textRange.EndPosition);
        this.radRichTextBox1.ChangeTextHighlightColor(Color.Red);
    }
}

I'd like to know how to get the line number of the textRange found and the entire text of the line that contains textRange.

Tommaso

6 Answers, 1 is accepted

Sort by
0
Svett
Telerik team
answered on 11 Oct 2012, 02:24 PM
Hi Tommaso,

It is not possible to determine the line number in the whole document. You can get the line number in the context of paragraph only. This restriction is caused because RadRichTextBox supports complex and nested layouts which cannot produce the line number as well as a pure text editor. You can get the line index inside the paragraph by using the following code snippet:
textRange.StartPosition.GetCurrentInlineBox().LineInfo.Paragraph

I hope this helps.
 
Regards,
Svett
the Telerik team
You’ve been asking for it and now it’s time for us to deliver. RadControls for WinForms Q3 2012 release is just around the corner. Sign up for a free webinar to see first all the latest enhancements.
0
Tommaso
Top achievements
Rank 1
answered on 11 Oct 2012, 03:26 PM
What property or method of
 
textRange.StartPosition.GetCurrentInlineBox().LineInfo.Paragraph

I must use to get line index?
0
Svett
Telerik team
answered on 16 Oct 2012, 09:51 AM
Hi Tommaso,

I posted the wrong code snippet. Please excuse me for the misunderstanding.

You can get the index in paragraph by using the following line:
textRange.StartPosition.GetCurrentInlineBox().LineInfo.IndexInParagraph

 Kind regards,
Svett
the Telerik team
You’ve been asking for it and now it’s time for us to deliver. RadControls for WinForms Q3 2012 release is just around the corner. Sign up for a free webinar to see first all the latest enhancements.
0
Tommaso
Top achievements
Rank 1
answered on 16 Oct 2012, 03:00 PM
Unfortunately the value I got is always 0.

The document I attach to the RichTextBox is a plain text document.
0
Accepted
Svett
Telerik team
answered on 19 Oct 2012, 11:52 AM
Hi Tommaso,

The IndexInParagraph property can return zero, if the paragraph contains only one line. You can calculate the line number of the word by using the following code snippet:
InlineLayoutBox box = textRange.StartPosition.GetCurrentInlineBox();
int index = box.LineInfo.IndexInParagraph;
int lineNumber = box.LineInfo.Paragraph.ChildIndex + index;

Note that the code above will work, if the document contains only paragraphs and text (excluding tables and images).
 
Regards,
Svett
the Telerik team
You’ve been asking for it and now it’s time for us to deliver. RadControls for WinForms Q3 2012 release is just around the corner. Sign up for a free webinar to see first all the latest enhancements.
0
Tommaso
Top achievements
Rank 1
answered on 19 Oct 2012, 12:43 PM
The snippet works if I add 1 to the lineNumber value.

Thanks for your support.
Tags
RichTextBox (obsolete as of Q3 2014 SP1)
Asked by
Tommaso
Top achievements
Rank 1
Answers by
Svett
Telerik team
Tommaso
Top achievements
Rank 1
Share this question
or