Version

Managing Find/Replace (xamRichTextEditor)

Topic Overview

Purpose

This topic explains the Find and Replace features of the xamRichTextEditor ™ control.

Required background

The following topics are prerequisites to understanding this topic:

Topic Purpose

This topic provides an overview of the features supported by the xamRichTextEditor control.

This topic explains the document’s content logical structure you can use to edit the contents in the xamRichTextEditor programmatically.

This topic provides detailed instructions to help you get up and running as quickly as possible with the xamRichTextEditor .

In this topic

This topic contains the following sections:

Find and Replace Summary

Find and replace summary chart

The xamRichTextEditor’s document supports text content find and replace operations. These functionalities are accessible using methods of the RichTextDocument and both the find and replace functionality use method overloads for operations over single or multiple document spans. In addition, both find and replace functionalities supports match case and wildcard search modes.

Note
Note

The wildcard support is the same as the pattern used by the Like operator in Visual Basic.

Configurable aspect Details Method(s)

Used to find document’s content

Used to replace document’s content

Find Text Content

Overview

You can use the FindAll method of the RichTextDocument to search the document’s content for specific text. The method returns an object of type FindResult that can be used to iterate over the matches of type FindMatch. Each of the FindMatch objects contains the document span occupied by the match.

Property settings

The following table maps the desired behaviors to its respective methods.

In order to: Use this method: And provide the following arguments:

Find text

FindAll

span – the document span to

textToFind – the text to search for

error – a string object that contains the error, if one occurs during the operation, such as when the textToFind wildcard is invalid in wildcard matching mode

findCriteria – an object of type FindCriteria, used to specify match case search or wildcard search mode

Example

The code snippet below demonstrates how to invoke the FindAll method for searching over the whole document span and iterating over all find matches:

In C#:

FindCriteria fc = new FindCriteria();
fc.CaseSensitive = this.cbCase.IsChecked ?? false;
fc.Operator =
    (this.cbWildcard.IsChecked.HasValue && this.cbWildcard.isChecked.Value) ?
    FindOperator.Wildcard : FindOperator.PlainText;
DocumentSpan ds = this.xamRichTextEditor1.Document.RootNode.Body.GetDocumentSpan();
string errorMsg;
FindResult fr = this.xamRichTextEditor1.Document.FindAll(ds,
    "text-to-find", out errorMsg, fc);
if (fr.HasMatch)
{
    foreach (FindMatch fm in fr.Matches)
    {
        // obtain document span for each find match
        DocumentSpan span = fm.Span;
    }
}

In Visual Basic:

Dim fc As New FindCriteria()
fc.CaseSensitive = If(Me.cbCase.IsChecked, False)
fc.[Operator] = _
    If((Me.cbWildcard.IsChecked.HasValue AndAlso Me.cbWildcard.isChecked.Value), _
    FindOperator.Wildcard, FindOperator.PlainText)
Dim ds As DocumentSpan = Me.xamRichTextEditor1.Document.RootNode.Body.GetDocumentSpan()
Dim errorMsg As String
Dim fr As FindResult = Me.xamRichTextEditor1.Document.FindAll(ds, "text-to-find", errorMsg, fc)
If fr.HasMatch Then
      For Each fm As FindMatch In fr.Matches
            ' obtain document span for each find match
            Dim span As DocumentSpan = fm.Span
      Next
End If

Replace Content

Overview

Use the ReplaceAll method of the RichTextDocument to replace specific text with text or content. To replace specific content use the ReplaceContents method. The replace operation takes place over the specified document span or spans. The ReplaceAll method returns an object of type ReplaceResult, used to iterate over the replacement items of type Replacement. The Replacement objects contain the find match and the replacement text and content.

Property settings

The following table maps the desired behaviors to its respective methods.

In order to: Use this method: And provide the following arguments:

Replace text

ReplaceAll

span/spans - the document span(s) over which to perform the replace opertation

textToFind – the text used for performing the search

replacementText/replacementContent – a string or NodeBase value used to replace the text

error – a string object containing the error, if one occurs during the operation

findCriteria – an object of type FindCriteria, used to specify the match case search or wildcard search mode

Replace content

ReplaceContent

span - the document span over which to perform the replace operation

newText/contentNode – a string or NodeBase value which will be used to replace the specified content

error – a string object containing the error, if one occurs during the operation

breakParagraphBeforeContent – set to true in order to include new paragraph before the new content

breakParagraphAfterContent – set to true to include new paragraph after the new content

Example

The code snippet below demonstrates how to invoke the ReplaceAll method for replacing a text content over the whole document span:

In C#:

FindCriteria fc = new FindCriteria();
fc.CaseSensitive = this.cbCase.IsChecked ?? false;
fc.Operator =
    (this.cbWildcard.IsChecked.HasValue && this.cbWildcard.isChecked.Value) ?
    FindOperator.Wildcard : FindOperator.PlainText;
DocumentSpan ds = this.xamRichTextEditor1.Document.RootNode.Body.GetDocumentSpan();
string errorMsg;
this.xamRichTextEditor1.Document.ReplaceAll(ds,
    "old-text", "new-text", out errorMsg, fc);

In Visual Basic:

Dim fc As New FindCriteria()
fc.CaseSensitive = If(Me.cbCase.IsChecked, False)
fc.[Operator] = _
    If((Me.cbWildcard.IsChecked.HasValue AndAlso Me.cbWildcard.isChecked.Value), _
    FindOperator.Wildcard, FindOperator.PlainText)
Dim ds As DocumentSpan = Me.xamRichTextEditor1.Document.RootNode.Body.GetDocumentSpan()
Dim errorMsg As String
Me.xamRichTextEditor1.Document.ReplaceAll(ds, "old-text", "new-text", errorMsg, fc)

Related Topics

The following topics provide additional information related to this topic.

Topic Purpose

This topic explains the xamRichTextEditor control’s clipboard operations from a developer’s perspective.

This topic covers the xamRichTextEditor control’s selection feature from the developer’s perspective.

This topic explains the xamRichTextEditor control’s Undo and Redo operations from a developer’s perspective.