Version

Methods for Modifying Text in a TextDocument

Append Text to the End of a Document – Code Example

Description

The Append method adds text to the end of the document, represented by the TextDocument class.

The following code example appends the text from a TextBox control to the end of the document.

Code

In C#:

_textDocument.Append(textBox.Text);

In Visual Basic:

_textDocument.Append(textBox.Text)

Delete Text from a Document – Code Example

Description

The Delete method, removes a specified span of continuous text, up to and including all of the text, within a document. It returns a Boolean, indicating the successful completion off the delete operation.

To delete all of the text, call the Delete method with no parameters.

The following code example deletes the first five characters from a document by passing “0” for the offset and “5” for the length to the Delete method. Upon successful deletion, it writes a message to the Visual Studio Output Window.

Code

In C#:

int offset = 0;
int length = 5;
if (_textDocument.Delete(offset, length))
    System.Diagnostics.Debug.WriteLine("Delete characters successful.");

In Visual Basic:

Dim offset As Integer = 0
Dim length As Integer = 5
If _textDocument.Delete(offset, length) Then
      System.Diagnostics.Debug.WriteLine("Delete characters successful.")
End If

Insert Text into a Document – Code Example

Description

The Insert method inserts text into the document at a specific offset.

The following code sample demonstrates how to insert the string “INSERTED TEXT” at an offset of 10 characters, resulting in the insertion of the text between the eleventh and twelfth character of the existing text.

Code

In C#:

_textDocument.Insert(10, "INSERTED TEXT");

In Visual Basic:

_textDocument.Insert(10, "INSERTED TEXT")

Replace All Occurrences of Found Text in a Document – Code Example

Description

The FindReplaceAll method finds and replaces all occurrences of the specified text after the specified offset location in the document. This method uses the TextSearchCritera object, which accepts either a verbatim string or a Regular Expression.

The following code example finds all occurrences of the less-than character “<” located at the beginning of a line in the document and replaces them with a pair of less-than characters “<<.”

Code

In C#:

TextSearchCriteria tsc = new TextSearchCriteria(true, "^<", RegexOptions.None);
_textDocument.FindReplaceAll(tsc,"<<");

In Visual Basic:

Dim tsc As New TextSearchCriteria(True, "^<", RegexOptions.None)
_textDocument.FindReplaceAll(tsc,"<<")

Replace a Single Occurrence of Text Found in a Document – Code Example

Description

The FindReplace method finds and replaces the first occurrence of the specified text string after the offset location specified in the document. This method uses the TextSearchCritera object, which accepts either a verbatim string or a Regular Expression.

The following code example finds the first occurrence of the less-than character “<” located at the beginning of a line, starting the search 50 characters into the text. It replaces the less than character “<” with a pair of less-than characters “<<”.

Code

In C#:

//Beginning 50 characters into the document, find the first occurrence of a less-than character at the beginning of a line.
//Replace it with a pair of less-than characters.
TextSearchCriteria tsc = new TextSearchCriteria(true, "^<", RegexOptions.None);
_textDocument.FindReplace("<<",tsc,50);

In Visual Basic:

'Beginning 50 characters into the document, find the first occurrence of a less-than character at the beginning of a line.
'Replace it with a pair of less-than characters.
Dim tsc As New TextSearchCriteria(True, "^<", RegexOptions.None)
_textDocument.FindReplace("<<", tsc, 0)

Replace Text at a Specific Location of the Document – Code Example

Description

The Replace method replaces the text located at a specific location as defined by its offset and length or a TextSpan object in the document. The length of the inserted string does not need to be equal to the length of the text that it is replacing.

The following code replaces the 10 characters of text beginning at the 100th character of the document (offset=99) with the string, “REPLACED TEXT”. The resulting document is three characters longer in this case, because a 13-character string replaces 10 characters of existing text.

Code

In C#:

_textDocument.Replace("REPLACED TEXT", 99, 10);

In Visual Basic:

_textDocument.Replace("REPLACED TEXT", 99, 10)

Related Content

Topics

The following topic provides additional information related to this topic.

Topic Purpose

This topic describes the Infragistics Syntax Parsing Engine’s main class, TextDocument, and contains links to topics that outline its most important methods, events and properties.

This topic uses descriptive text and code snippets to illustrate the TextDocument class events.

This topic uses descriptive text to illustrate the TextDocument class properties exposed to enable custom configuration.

This topic uses descriptive text and code snippets to illustrate how to provide use a RichTextBox control to highlight keywords in a document according to a specific language.