DocumentRange Interface
Defines a document range.
Namespace: DevExpress.XtraRichEdit.API.Native
Assembly: DevExpress.RichEdit.v19.1.Core.dll
Declaration
Related API Members
The following members return DocumentRange objects:
Remarks
The range is a distance between two positions (DocumentRange.Start and DocumentRange.End). The difference between the end and start position defines the DocumentRange.Length.
Insert and Delete Text
Text can be added to the range by the SubDocument.AppendText, SubDocument.InsertText or SubDocument.InsertSingleLineText methods. The DocumentRange.End) and DocumentRange.Length property values increase when new text elements are added.
Use the SubDocument.Delete method to delete characters from the range. After deletion, the DocumentRange.End) and DocumentRange.Length property values will lessen.
If the text preceding the range is removed, the range’s start and end positions move backward, i.e., the DocumentRange.Start and DocumentRange.End values decrease. The DocumentRange.Length value remains the same.
The code sample below obtains a range and calls the SubDocument.InsertText method to insert a text as follows:
ABNewTextCDEFGH
Range r1 starts at 1, ends at 11
Range r2 starts at 2, ends at 9
Note
A complete sample project is available at https://github.com/DevExpress-Examples/winforms-richedit-document-api-e5219
document.AppendText("ABCDEFGH")
Dim r1 As DocumentRange = document.CreateRange(1, 3)
Dim pos1 As DocumentPosition = document.CreatePosition(2)
Dim r2 As DocumentRange = document.InsertText(pos1, ">>NewText<<")
Dim s1 As String = String.Format("Range r1 starts at {0}, ends at {1}", r1.Start, r1.End)
Dim s2 As String = String.Format("Range r2 starts at {0}, ends at {1}", r2.Start, r2.End)
document.Paragraphs.Append()
document.AppendText(s1)
document.Paragraphs.Append()
document.AppendText(s2)
Fixed Range
The range returned by the SubDocument.AppendText method extends after each operation. This behavior may complicate certain situations.
You can implement an extension to the DocumentRange class to provide fixed ranges. Fixed ranges remain unchanged after they are obtained. The following code illustrates the CustomFixedRange implementation.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/how-to-create-a-fixed-reference-to-an-existing-document-range-the-fixed-range-feature-e5207
using System;
using System.Drawing;
using DevExpress.XtraRichEdit.API.Native;
public static class FixedRangeExtension
{
public static CustomFixedRange GetFixedRange(this DocumentRange range)
{
return new CustomFixedRange(range);
}
public static CharacterProperties BeginUpdateCharacters
(this Document document, CustomFixedRange range)
{
return document.BeginUpdateCharacters(range.CreateRange(document));
}
}
public class CustomFixedRange
{
int start;
int length;
public CustomFixedRange(DocumentRange range)
{
this.start = range.Start.ToInt();
this.length = range.Length;
}
public DocumentRange CreateRange(Document document)
{
return document.CreateRange(start, length);
}
}
This code snippet demonstrates how to format each appended text block using CustomFixedRange.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/how-to-create-a-fixed-reference-to-an-existing-document-range-the-fixed-range-feature-e5207
Document document = richEditControl1.Document;
document.AppendText("First text block without formatting. ");
var formattedRangeBold = document.AppendText("Second text block with bold font. ").GetFixedRange();
document.AppendText("Third text block without formatting. ");
var formattedRangeUnderlined = document.AppendText("Fourth text block with underlined font. ").GetFixedRange();
var charsBold = document.BeginUpdateCharacters(formattedRangeBold);
charsBold.Bold = true;
document.EndUpdateCharacters(charsBold);
var charsUnderline = document.BeginUpdateCharacters(formattedRangeUnderlined);
charsUnderline.Underline = UnderlineType.Single;
charsUnderline.UnderlineColor = Color.Brown;
document.EndUpdateCharacters(charsUnderline);
Related GitHub Examples
The following code snippets (auto-collected from DevExpress Examples) contain references to the DocumentRange interface.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.