Edit this page

Search

RadRichTextBox supports searching the contents of the document along with providing some methods for manipulating the selection. Used in combination, they become quite a powerful tool enabling scenarios like highlighting of specific parts of the document or replacing words and collocations.

note

To learn more about the selection API read theSelectiontopic.

The simplest scenario – finding a string in the content of the document can be implemented with the following code:

Select all matches

private void SelectAllMatches(string toSearch)
{
    this.radRichTextBox1.Document.Selection.Clear(); // this clears the selection before processing
    DocumentTextSearch search = new DocumentTextSearch(this.radRichTextBox1.Document);
    foreach (var textRange in search.FindAll(toSearch))
    {
        this.radRichTextBox1.Document.Selection.AddSelectionStart(textRange.StartPosition);
        this.radRichTextBox1.Document.Selection.AddSelectionEnd(textRange.EndPosition);
    }
}

Private Sub SelectAllMatches(ByVal toSearch As String)
    Me.RadRichTextBox1.Document.Selection.Clear() ' this clears the selection before processing
    Dim search As New DocumentTextSearch(Me.RadRichTextBox1.Document)
    For Each textRange In search.FindAll(toSearch)
        Me.RadRichTextBox1.Document.Selection.AddSelectionStart(textRange.StartPosition)
        Me.RadRichTextBox1.Document.Selection.AddSelectionEnd(textRange.EndPosition)
    Next textRange
End Sub

note

Note that the FindAll() method of the DocumentTextSearch class takes a string as an argument, but regards it as a regular expression. For example, searching “ [a-z]control[a-z] ”will find all words that contain the word “ control ” as a substring. Thus, if you have the word “ RadControls ” in the document, executing the SelectAllMatches(“control”) will result in only these six letters being selected, as opposed to the whole word “ RadControls ” being selected when SelectAllMatches(“[a-z]control[a-z]”) .

You can use all kind of regular expressions, such as “(asp|silverlight)\s*control(s)?” (which would match both “ASP Controls” and “SilverlightControl”), [a-z](.)\1[a-z] (which finds all words that have a doubled letter), and basically everything else you can come up with. Make sure to use correct expressions if you want to utilize this functionality, or escape the string in case you want a simple search for a word that can contain special symbols like “?”, “+”, “{“ etc. This can be automatically done by invoking the following code before proceeding with the search.

Escape string

toSearch = Regex.Escape(toSearch);

toSearch = Regex.Escape(toSearch)

Selecting the results of the search is particularly useful, as most formatting commands that are executed on RadRichTextBox (or its document) are selection-based, i.e. they operate on the currently selected parts of the document. You can use that fact by invoking one of these commands after having selected the words in the document. Here is an example.

Customize selection

this.radRichTextBox1.ChangeTextHighlightColor(Color.LightGray);  // will highlight all selected words in LightGray
this.radRichTextBox1.ChangeFontSize(Unit.PointToDip(32));   // will increase the font size of the words to 30 DIP
this.radRichTextBox1.ChangeFontFamily("Comic Sans MS"); // will change the font family of the spans, containing these words.

Me.RadRichTextBox1.ChangeTextHighlightColor(Color.LightGray) ' will highlight all selected words in LightGray
Me.RadRichTextBox1.ChangeFontSize(Unit.PointToDip(32)) ' will increase the font size of the words to 30 DIP
Me.RadRichTextBox1.ChangeFontFamily("Comic Sans MS") ' will change the font family of the spans, containing these words.

Most of the time, you would like to remove the selection at the end of the operations, which is done by calling the Clear() method of the DocumentSelection object.

Clear selection

this.radRichTextBox1.Document.Selection.Clear();

Me.RadRichTextBox1.Document.Selection.Clear()

warning

You should have in mind that performing these actions in DocumentContentChanged event handler will not only raise performance issues, but will also produce an infinite loop, as the changes of the formatting are also considered document changes.

On a side note, the find and select functionality can be extended in order to implement a replace of all occurrences matching the searched string with another string. Here is a sample code:

Replace all matches

private void ReplaceAllMatches(string toSearch, string toReplaceWith)
{
    this.radRichTextBox1.Document.Selection.Clear(); // this clears the selection before processing
    DocumentTextSearch search = new DocumentTextSearch(this.radRichTextBox1.Document);
    List<TextRange> rangesTrackingDocumentChanges = new List<TextRange>();
    foreach (var textRange in search.FindAll(toSearch))
    {
        TextRange newRange = new TextRange(new DocumentPosition(textRange.StartPosition, true), new DocumentPosition(textRange.EndPosition, true));
        rangesTrackingDocumentChanges.Add(newRange);
    }
    foreach (var textRange in rangesTrackingDocumentChanges)
    {
        this.radRichTextBox1.Document.Selection.AddSelectionStart(textRange.StartPosition);
        this.radRichTextBox1.Document.Selection.AddSelectionEnd(textRange.EndPosition);
        this.radRichTextBox1.Insert(toReplaceWith);
        textRange.StartPosition.Dispose();
        textRange.EndPosition.Dispose();
    }
}

Private Sub ReplaceAllMatches(ByVal toSearch As String, ByVal toReplaceWith As String)
    Me.RadRichTextBox1.Document.Selection.Clear() ' this clears the selection before processing
    Dim search As New DocumentTextSearch(Me.RadRichTextBox1.Document)
    Dim rangesTrackingDocumentChanges As New List(Of TextRange)()
    For Each textRange In search.FindAll(toSearch)
        Dim newRange As New TextRange(New DocumentPosition(textRange.StartPosition, True), New DocumentPosition(textRange.EndPosition, True))
        rangesTrackingDocumentChanges.Add(newRange)
    Next textRange
    For Each textRange In rangesTrackingDocumentChanges
        Me.RadRichTextBox1.Document.Selection.AddSelectionStart(textRange.StartPosition)
        Me.RadRichTextBox1.Document.Selection.AddSelectionEnd(textRange.EndPosition)
        Me.RadRichTextBox1.Insert(toReplaceWith)
        textRange.StartPosition.Dispose()
        textRange.EndPosition.Dispose()
    Next textRange
End Sub

There is a need to create new TextRanges with document positions that track the changes to the document, because the deletion and insertion affects the document positions. The document positions created using the 2-parameter constructor with the second parameter set to “true” keep their relative position as the document changes. They, however, have to be manually disposed of, as they subscribe to events in order to track the changes to the document and must detach their handlers at the end of their use. This is done using the Dispose() method. If you skip that step, there will be memory leaks in your application.

Was this article helpful? Yes No
Thank you for your feedback!

Give article feedback

Tell us how we can improve this article

close
Dummy