Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 56 additions & 26 deletions Document-Processing/Word/Word-Processor/wpf/Find-and-Replace.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ description: Learn here all about Find and Replace support in Syncfusion WPF Ric
platform: document-processing
control: SfRichTextBoxAdv
documentation: ug
keywords: search,find,replace-text
keywords: search,find,replace-text,find-options,regex-find
---
# Find and Replace in WPF RichTextBox (SfRichTextBoxAdv)

The [WPF RichTextBox](https://www.syncfusion.com/docx-editor-sdk/wpf-docx-editor) (SfRichTextBoxAdv) control supports searching text contents in the document. This when used in combination with selection becomes a powerful tool enabling scenarios like highlighting specific parts of the document, applying formatting such as bold or replacing text. You can extend your search by using regular expression to find particular pattern of text in the document.
The [WPF RichTextBox](https://www.syncfusion.com/docx-editor-sdk/wpf-docx-editor) (SfRichTextBoxAdv) control supports searching text contents in the document. When combined with selection, it becomes a powerful tool for highlighting specific parts of the document, applying formatting such as bold, or replacing text. You can extend your search by using regular expressions to find a particular pattern of text in the document.

The find and replace operations are exposed through the [Find](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.RichTextBoxAdv.SfRichTextBoxAdv.html#Syncfusion_Windows_Controls_RichTextBoxAdv_SfRichTextBoxAdv_Find_System_String_Syncfusion_Windows_Controls_RichTextBoxAdv_FindOptions_), [FindAll](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.RichTextBoxAdv.SfRichTextBoxAdv.html#Syncfusion_Windows_Controls_RichTextBoxAdv_SfRichTextBoxAdv_FindAll_System_String_Syncfusion_Windows_Controls_RichTextBoxAdv_FindOptions_), and [FindOptions](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.RichTextBoxAdv.FindOptions.html) API members.

## Find

The following code example explains how to find the first occurrence of a particular text in the document and apply bold formatting.
{% tabs %}
{% highlight c# %}
// Finds the first occurrence of specified text that matches case in RichTextBoxAdv document from current selection.
// Finds the first occurrence of a specified text that matches case in SfRichTextBoxAdv document from current selection.
TextSearchResult textSearchResult = richTextBoxAdv.Find("Panda", FindOptions.CaseSensitive);

// Selects the text search result.
Expand All @@ -23,11 +28,11 @@ richTextBoxAdv.Selection.CharacterFormat.Bold = true;

{% endhighlight %}
{% highlight VB %}
' Finds the first occurrence of specified text that matches case in RichTextBoxAdv document from current selection.
' Finds the first occurrence of a specified text that matches case in SfRichTextBoxAdv document from current selection.
Dim textSearchResult As TextSearchResult = richTextBoxAdv.Find("Panda", FindOptions.CaseSensitive)

' Selects the text search result.
richTextBoxAdv.Selection.[Select](textSearchResult.Start, textSearchResult.[End])
richTextBoxAdv.Selection.Select(textSearchResult.Start, textSearchResult.[End])

' Applies Bold formatting for the current selection.
richTextBoxAdv.Selection.CharacterFormat.Bold = True
Expand All @@ -37,56 +42,71 @@ richTextBoxAdv.Selection.CharacterFormat.Bold = True

{% endtabs %}

The following code example demonstrates how to find all occurrences of a particular pattern of text in the document and highlighting the result.
The `Find` method accepts a [FindOptions](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.RichTextBoxAdv.FindOptions.html) value that controls how the search is performed. The following flags are available:

| Flag | Description |
|------|-------------|
| `None` | Performs a case-insensitive search without whole-word matching (default behavior). |
| `CaseSensitive` | Performs a search that matches the exact case of the search text. |
| `WholeWord` | Performs a search that matches only whole words. |

N> When `CaseSensitive` is not specified, the search is case-insensitive by default.

## Find all

The following code example demonstrates how to find all occurrences of a particular pattern of text in the document and highlight the result.
{% tabs %}
{% highlight c# %}
// Finds all the words that starts with ‘S’ in RichTextBoxAdv document.
// Finds all the words that start with ‘S’ in SfRichTextBoxAdv document.
TextSearchResults textSearchResults = richTextBoxAdv.FindAll(new Regex(@"\bS\S*"), FindOptions.None);

// If any text search results 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);
}
// Apply highlight color for the selection.

// Applies highlight color to all selected ranges.
richTextBoxAdv.Selection.CharacterFormat.HighlightColor = HighlightColor.Yellow;
}

{% endhighlight %}
{% highlight VB %}
' Finds all the words that starts with ‘S’ in RichTextBoxAdv document.
' Finds all the words that start with ‘S’ in SfRichTextBoxAdv document.
Dim textSearchResults As TextSearchResults = richTextBoxAdv.FindAll(New Regex("\bS\S*"), FindOptions.None)

' If any text search results found.
If textSearchResults IsNot Nothing Then
For j As Integer = 0 To textSearchResults.Count - 1

Dim textSearchResult As TextSearchResult = textSearchResults(j)
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
' Adds the search result text positions to the selection.
richTextBoxAdv.Selection.SelectionRanges.Add(textSearchResult.Start, textSearchResult.[End])
Next

' Apply highlight color for the selection.
richTextBoxAdv.Selection.CharacterFormat.HighlightColor = HighlightColor.Yellow
' Applies highlight color to all selected ranges.
richTextBoxAdv.Selection.CharacterFormat.HighlightColor = HighlightColor.Yellow
End If


{% endhighlight %}

{% endtabs %}

The example above applies a [HighlightColor](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.RichTextBoxAdv.HighlightColor.html) to the selected ranges. The supported values are `Black`, `Blue`, `Cyan`, `Green`, `Magenta`, `Red`, `Yellow`, `White`, and `None` (which clears an existing highlight).

## Replacing existing text

You can replace a single occurrence or all occurrences of a particular text or pattern of text in a document with another text by performing search operation. This helps you to modify the contents easily.
The following code example demonstrates how to replace a single occurrence of a text with another text in SfRichTextBoxAdv.
You can replace a single occurrence or all occurrences of a particular text or pattern of text in a document with another text by performing a search operation. This makes it easy to modify the document contents.

### Replace a single occurrence

The following code example demonstrates how to replace a single occurrence of a text with another text in SfRichTextBoxAdv. The [`TextSearchResult.Replace(string)`](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.RichTextBoxAdv.TextSearchResults.html#Syncfusion_Windows_Controls_RichTextBoxAdv_TextSearchResults_Replace_System_String_) method takes the replacement text as its argument and replaces the matched text in the current result.
{% tabs %}
{% highlight c# %}
// Finds the text "colour" that matches whole word in the document.
Expand All @@ -111,7 +131,9 @@ End If

{% endtabs %}

The following code example demonstrates how to replace all occurrences of a particular text with another text in SfRichTextBoxAdv.
### Replace all occurrences

The following code example demonstrates how to replace all occurrences of a particular text with another text in SfRichTextBoxAdv. The [`TextSearchResults.ReplaceAll(string)`](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.RichTextBoxAdv.TextSearchResults.html#Syncfusion_Windows_Controls_RichTextBoxAdv_TextSearchResults_ReplaceAll_System_String_) method takes the replacement text as its argument and applies it to every match returned by `FindAll`.
{% tabs %}
{% highlight c# %}
// Finds the text "analyse" that matches whole word in the document.
Expand All @@ -138,8 +160,10 @@ End If

## Options Pane

The SfRichTextBoxAdv provides the built-in options pane support to find the text and navigate to text results similar to Microsoft Word application.
The following code example demonstrates how to show the options pane in SfRichTextBoxAdv through command binding.
The SfRichTextBoxAdv provides built-in options pane support to find text and navigate through the search results, similar to that of Microsoft Word.

The following code example demonstrates how to show the options pane in SfRichTextBoxAdv through the [ShowOptionsPaneCommand](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.RichTextBoxAdv.SfRichTextBoxAdv.html#Syncfusion_Windows_Controls_RichTextBoxAdv_SfRichTextBoxAdv_ShowOptionsPaneCommand) routed UI command.

{% tabs %}
{% highlight xaml %}
<!-- Binding Button to UI Command that shows the options pane -->
Expand All @@ -149,6 +173,12 @@ The following code example demonstrates how to show the options pane in SfRichTe
{% endhighlight %}

{% endtabs %}
![WPF RichTextBox displays Find option](Find-and-Replace_images/wpf-richtextbox-find-option.jpeg)
![WPF RichTextBox displays the Find option pane with the search input and navigation controls](Find-and-Replace_images/wpf-richtextbox-find-option.jpeg)

N> You can refer to our [WPF RichTextBox](https://www.syncfusion.com/docx-editor-sdk/wpf-docx-editor) feature tour page for its groundbreaking feature representations. You can also explore our [WPF RichTextBox example](https://github.com/syncfusion/docx-editor-sdk-wpf-demos) to know how to render and configure the editing tool.

## See Also

N> You can refer to our [WPF RichTextBox](https://www.syncfusion.com/docx-editor-sdk/wpf-docx-editor) feature tour page for its groundbreaking feature representations.You can also explore our [WPF RichTextBox example](https://github.com/syncfusion/docx-editor-sdk-wpf-demos) to knows how to render and configure the editing tools.
- [Selection in WPF RichTextBox](./Selection)
- [Commands in WPF RichTextBox](./Commands)
- [Document Structure in WPF RichTextBox](./Document-Structure)
41 changes: 33 additions & 8 deletions Document-Processing/Word/Word-Processor/wpf/Hyperlink.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ description: Learn here all about Hyperlink support in Syncfusion WPF RichTextBo
platform: document-processing
control: SfRichTextBoxAdv
documentation: ug
keywords: hyperlink
keywords: hyperlink,insert-hyperlink,screen-tip,request-navigate
---
# Hyperlink in WPF RichTextBox (SfRichTextBoxAdv)

The [WPF RichTextBox](https://www.syncfusion.com/docx-editor-sdk/wpf-docx-editor) (SfRichTextBoxAdv) supports hyperlink field similar to the Microsoft Word. You can link part of the document content to Internet or file location, mail address or any text.
The [WPF RichTextBox](https://www.syncfusion.com/docx-editor-sdk/wpf-docx-editor) (SfRichTextBoxAdv) supports hyperlink fields similar to Microsoft Word. You can link part of the document content to the Internet, a file location, an email address, or any other text.

## Insert hyperlink

The following code example illustrates how to insert a hyperlink field.

A HYPERLINK field consists of five parts: [FieldBeginAdv](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.RichTextBoxAdv.FieldBeginAdv.html), the field code text in a [SpanAdv](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.RichTextBoxAdv.SpanAdv.html), [FieldSeparatorAdv](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.RichTextBoxAdv.FieldSeparatorAdv.html), the field result text in another [SpanAdv](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.RichTextBoxAdv.SpanAdv.html), and [FieldEndAdv](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.RichTextBoxAdv.FieldEndAdv.html).

{% tabs %}
{% highlight xaml %}
<RichTextBoxAdv:ParagraphAdv>
Expand Down Expand Up @@ -70,7 +76,7 @@ paragraphAdv.Inlines.Add(New FieldEndAdv())
{% endhighlight %}
{% endtabs %}

The following code example illustrates how to insert hyperlink field into SfRichTextBoxAdv Document through UI command.
The following code example illustrates how to insert hyperlink field into SfRichTextBoxAdv Document through the [InsertHyperlinkCommand](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.RichTextBoxAdv.SfRichTextBoxAdv.html#Syncfusion_Windows_Controls_RichTextBoxAdv_SfRichTextBoxAdv_InsertHyperlinkCommand) UI command. The `CommandParameter` accepts a URL string that the hyperlink will point to.
{% tabs %}
{% highlight xaml %}
<Button Content="Insert Hyperlink" Command="RichTextBoxAdv:SfRichTextBoxAdv.InsertHyperlinkCommand" CommandTarget="{Binding ElementName=richTextBoxAdv}" CommandParameter="www.google.com"/>
Expand All @@ -82,11 +88,13 @@ The following code example illustrates how to insert hyperlink field into SfRich

## Hyperlink ScreenTip

In RichTextBox control ToolTip (ScreenTip) shows some information or navigation link, when the mouse hovers over that hyperlink and it disappears when the mouse is moved away from that hyperlink. By default, it shows navigation link of that hyperlink and you can set the text you want to use for your ScreenTip.
In the SfRichTextBoxAdv control, a ToolTip (ScreenTip) shows additional information, such as the navigation link, when the mouse hovers over a hyperlink, and disappears when the mouse moves away. "ScreenTip" is the WPF RichTextBoxAdv term for the hyperlink tooltip; the underlying WPF `ToolTip` is used to display it. By default, it shows the navigation link of that hyperlink, and you can set the text you want to use for the ScreenTip.

N> In the HYPERLINK field code, the optional switch `\o "<text>"` sets the ScreenTip text for the hyperlink.

<table><tr><td>Without ScreenTipText</td><td>With ScreenTipText</td></tr><tr><td><img src="Hyperlink_images/screentip_ug1.PNG" alt="Hyperlink_images1"/></td><td><img src="Hyperlink_images/screentip_ug2.PNG" alt="Hyperlink_images2"/></td></tr></table>

The following code example illustrates how to insert a hyperlink field with ScreenTip.
The following code example illustrates how to insert a hyperlink field with a ScreenTip.
{% tabs %}
{% highlight xaml %}
<RichTextBoxAdv:ParagraphAdv>
Expand Down Expand Up @@ -149,6 +157,11 @@ paragraphAdv.Inlines.Add(New FieldEndAdv())
{% endtabs %}

The following code example illustrates how to insert hyperlink field with ScreenTip into RichTextBox Document through UI command.

### Insert with ScreenTip via UI command

The `string[3]` parameter array contains the URL, the display text, and the ScreenTip text, in that order.

{% tabs %}
{% highlight C# %}
SfRichTextBoxAdv.InsertHyperlinkCommand.Execute(new string[3] { "www.syncfusion.com", "SfRichTextBoxAdv", "SfRichTextBox" }, richTextBoxAdv);
Expand All @@ -175,7 +188,7 @@ The following section illustrates how to insert hyperlink field with ScreenTip i

![Adding Hyperlink to WPF RichTextBox](Hyperlink_images/wpf-richtextbox-insert-hyperlink.PNG)

In the SfRichTextBoxAdv control, ToolTip (ScreenTip) will be shown by default when the mouse hovers over that hyperlink. You can disable ToolTip by using the [DisplayScreenTips](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.RichTextBoxAdv.EditorSettings.html#Syncfusion_Windows_Controls_RichTextBoxAdv_EditorSettings_DisplayScreenTips) property of [EditorSettings](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.RichTextBoxAdv.EditorSettings.html) class.
In the SfRichTextBoxAdv control, the ToolTip (ScreenTip) is shown by default when the mouse hovers over a hyperlink. You can disable the ToolTip by setting the [DisplayScreenTips](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.RichTextBoxAdv.EditorSettings.html#Syncfusion_Windows_Controls_RichTextBoxAdv_EditorSettings_DisplayScreenTips) property of the [EditorSettings](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.RichTextBoxAdv.EditorSettings.html) class. Set it to `false` to disable the ScreenTip; the default is `true`.

The following code example illustrates how to disable the ToolTip of the hyperlink.

Expand Down Expand Up @@ -204,8 +217,12 @@ N> ScreenTip option is supported from V18.4.0.30

## Hyperlink Navigation

The SfRichTextBoxAdv supports event to identify whenever hyperlink navigation is requested. This allows you to easily customize the hyperlink navigation functionality.
The SfRichTextBoxAdv supports an event that is raised when hyperlink navigation is requested. You can use this event to customize hyperlink navigation.

The [RequestNavigate](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.RichTextBoxAdv.SfRichTextBoxAdv.html#Syncfusion_Windows_Controls_RichTextBoxAdv_SfRichTextBoxAdv_RequestNavigate) event provides the [RequestNavigateEventArgs](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.RichTextBoxAdv.RequestNavigateEventArgs.html) which exposes the [Hyperlink](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.RichTextBoxAdv.Hyperlink.html) being navigated to. The following example demonstrates how to handle this event.

The following code example demonstrates how to customize hyperlink navigation functionality for the SfRichTextBoxAdv instance.

{% tabs %}
{% highlight c# %}
// Hooks the event handler for RequestNavigate event.
Expand All @@ -224,6 +241,8 @@ private void RichTextBoxAdv_RequestNavigate(object obj, Syncfusion.Windows.Contr
Process.Start(args.Hyperlink.NavigationLink);
}

N> The available [HyperlinkType](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.RichTextBoxAdv.HyperlinkType.html) values are `Webpage`, `Email`, `File` and `Bookmark`. The [NavigationLink](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.RichTextBoxAdv.Hyperlink.html#Syncfusion_Windows_Controls_RichTextBoxAdv_Hyperlink_NavigationLink) property contains the destination of the hyperlink as a string.

// Unhooks the event handler for RequestNavigate event.
richTextBoxAdv.RequestNavigate -= RichTextBoxAdv_RequestNavigate;

Expand Down Expand Up @@ -254,4 +273,10 @@ RemoveHandler richTextBoxAdv.RequestNavigate, AddressOf RichTextBoxAdv_RequestNa
{% endhighlight %}
{% endtabs %}

N> You can refer to our [WPF RichTextBox](https://www.syncfusion.com/docx-editor-sdk/wpf-docx-editor) feature tour page for its groundbreaking feature representations.You can also explore our [WPF RichTextBox example](https://github.com/syncfusion/docx-editor-sdk-wpf-demos) to knows how to render and configure the editing tools.
N> You can refer to our [WPF RichTextBox](https://www.syncfusion.com/docx-editor-sdk/wpf-docx-editor) feature tour page for its groundbreaking feature representations. You can also explore our [WPF RichTextBox example](https://github.com/syncfusion/docx-editor-sdk-wpf-demos) to know how to render and configure the editing tool.

## See Also

- [Selection in WPF RichTextBox](./Selection)
- [Commands in WPF RichTextBox](./Commands)
- [Document Structure in WPF RichTextBox](./Document-Structure)
Loading