Find and Replace in UWP DOCX Editor
18 Aug 20267 minutes to read
The SfRichTextBoxAdv control supports searching text content in the document. When used in combination with selection, this becomes a powerful tool that enables scenarios such as highlighting specific parts of the document, applying formatting (for example, bold), or replacing text. You can extend your search by using regular expressions to find a particular pattern of text in the document.
Finding text
The following code example explains how to find the first occurrence of a particular text in the document and apply bold formatting.
// Finds the first occurrence of the specified text, matching case, in the SfRichTextBoxAdv document from the current selection.
TextSearchResult textSearchResult = richTextBoxAdv.Find("Panda", FindOptions.CaseSensitive);
// Selects the text search result.
richTextBoxAdv.Selection.Select(textSearchResult.Start, textSearchResult.End);
// Applies bold formatting to the current selection.
richTextBoxAdv.Selection.CharacterFormat.Bold = true;' Finds the first occurrence of the specified text, matching case, in the SfRichTextBoxAdv document from the current selection.
Dim textSearchResult As TextSearchResult = richTextBoxAdv.Find("Panda", FindOptions.CaseSensitive)
' Selects the text search result.
richTextBoxAdv.Selection.Select(textSearchResult.Start, textSearchResult.End)
' Applies bold formatting to the current selection.
richTextBoxAdv.Selection.CharacterFormat.Bold = TrueThe following code example demonstrates how to find all occurrences of a particular pattern of text in the document and highlight the result.
// Finds all the words that start with 'S' in the SfRichTextBoxAdv document.
TextSearchResults textSearchResults = richTextBoxAdv.FindAll(new Regex(@"\bS\S*"), FindOptions.None);
// If any text search results are found.
if (textSearchResults != null)
{
for (int j = 0; j < textSearchResults.Count; j++)
{
TextSearchResult textSearchResult = textSearchResults[j];
// Adds the search result text positions to the selection.
richTextBoxAdv.Selection.SelectionRanges.Add(textSearchResult.Start, textSearchResult.End);
}
// Applies the highlight color to the selection.
richTextBoxAdv.Selection.CharacterFormat.HighlightColor = HighlightColor.Yellow;
}' Finds all the words that start with 'S' in the SfRichTextBoxAdv document.
Dim textSearchResults As TextSearchResults = richTextBoxAdv.FindAll(New Regex("\bS\S*"), FindOptions.None)
' If any text search results are found.
If textSearchResults IsNot Nothing Then
For j As Integer = 0 To textSearchResults.Count - 1
Dim textSearchResult As TextSearchResult = textSearchResults(j)
' Adds the search result text positions to the selection.
richTextBoxAdv.Selection.SelectionRanges.Add(textSearchResult.Start, textSearchResult.End)
Next
' Applies the highlight color to the selection.
richTextBoxAdv.Selection.CharacterFormat.HighlightColor = HighlightColor.Yellow
End IfReplacing text
You can replace a single occurrence or all occurrences of a particular text or pattern in a document with another text by performing a search operation. This helps you to modify the content easily.
The following code example demonstrates how to replace a single occurrence of a text with another text in SfRichTextBoxAdv.
// Finds the text "flat" that matches the whole word in the document.
TextSearchResult textSearchResult = richTextBoxAdv.Find("flat", FindOptions.WholeWord);
// If any text search result is found, replace it with the text "apartment".
if (textSearchResult != null)
textSearchResult.Replace("apartment");' Finds the text "flat" that matches the whole word in the document.
Dim textSearchResult As TextSearchResult = richTextBoxAdv.Find("flat", FindOptions.WholeWord)
' If any text search result is found, replace it with the text "apartment".
If textSearchResult IsNot Nothing Then
textSearchResult.Replace("apartment")
End IfThe following code example demonstrates how to replace all occurrences of a particular text with another text in SfRichTextBoxAdv.
// Finds the text "lift" that matches the whole word in the document.
TextSearchResults textSearchResults = richTextBoxAdv.FindAll("lift", FindOptions.WholeWord);
// If any text search results are found, replace all occurrences with the text "elevator".
if (textSearchResults != null)
textSearchResults.ReplaceAll("elevator");' Finds the text "lift" that matches the whole word in the document.
Dim textSearchResults As TextSearchResults = richTextBoxAdv.FindAll("lift", FindOptions.WholeWord)
' If any text search results are found, replace all occurrences with the text "elevator".
If textSearchResults IsNot Nothing Then
textSearchResults.ReplaceAll("elevator")
End IfOptions pane
SfRichTextBoxAdv provides a built-in options pane to find text and navigate to the results, similar to the Microsoft Word application. It also provides a separate pane for advanced find and replace options.
The following code example demonstrates how to show the default search pane in SfRichTextBoxAdv through command binding.
<!-- Binds the button to the UI command that shows the default search pane. -->
<Button Content="Show Search Pane" Command="{Binding ElementName=richTextBoxAdv, Path=ShowOptionsPaneCommand}" />
The following code example demonstrates how to show the advanced find pane in SfRichTextBoxAdv through command binding.
<!-- Binds the button to the UI command that shows the advanced find pane. -->
<Button Content="Show Advanced Find Pane" Command="{Binding ElementName=richTextBoxAdv, Path=ShowOptionsPaneCommand}" CommandParameter="Find" />
The following code example demonstrates how to show the advanced replace pane in SfRichTextBoxAdv through command binding.
<!-- Binds the button to the UI command that shows the advanced replace pane. -->
<Button Content="Show Advanced Replace Pane" Command="{Binding ElementName=richTextBoxAdv, Path=ShowOptionsPaneCommand}" CommandParameter="Replace" />
NOTE
Currently, the SfRichTextBoxAdv does not support the options pane on Phone devices.