How to get line number

7 posts, 1 answers
  1. Tommaso
    Tommaso avatar
    5 posts
    Member since:
    Oct 2012

    Posted 08 Oct 2012 Link to this post

    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
  2. Svett
    Admin
    Svett avatar
    728 posts

    Posted 11 Oct 2012 Link to this post

    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.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Tommaso
    Tommaso avatar
    5 posts
    Member since:
    Oct 2012

    Posted 11 Oct 2012 Link to this post

    What property or method of
     
    textRange.StartPosition.GetCurrentInlineBox().LineInfo.Paragraph

    I must use to get line index?
  5. Svett
    Admin
    Svett avatar
    728 posts

    Posted 16 Oct 2012 Link to this post

    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.
  6. Tommaso
    Tommaso avatar
    5 posts
    Member since:
    Oct 2012

    Posted 16 Oct 2012 Link to this post

    Unfortunately the value I got is always 0.

    The document I attach to the RichTextBox is a plain text document.
  7. Answer
    Svett
    Admin
    Svett avatar
    728 posts

    Posted 19 Oct 2012 Link to this post

    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.
  8. Tommaso
    Tommaso avatar
    5 posts
    Member since:
    Oct 2012

    Posted 19 Oct 2012 Link to this post

    The snippet works if I add 1 to the lineNumber value.

    Thanks for your support.
Back to Top
UI for WinForms is Visual Studio 2017 Ready