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

Find and Highlight text by code

1 Answer 239 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 04 Oct 2012, 10:16 AM
Q1 2012
VS 2010

The following snippet does not run.

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


How can I highlight with red color all the occurrences of the text?

Tommaso

1 Answer, 1 is accepted

Sort by
0
Accepted
Svett
Telerik team
answered on 08 Oct 2012, 02:40 PM
Hi Tommaso,

Thank you for writing.

You can use the following code snippet to select the desired words in the RadRichTextBox:
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 hope this helps.

Greetings,
Svett
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
Tags
RichTextBox (obsolete as of Q3 2014 SP1)
Asked by
Tommaso
Top achievements
Rank 1
Answers by
Svett
Telerik team
Share this question
or