Skip to main content

DocumentRange Interface

Defines a document range.

Namespace: DevExpress.XtraRichEdit.API.Native

Assembly: DevExpress.RichEdit.v24.1.Core.dll

NuGet Packages: DevExpress.RichEdit.Core, DevExpress.Win.Navigation

Declaration

[ComVisible(true)]
public interface DocumentRange

The following members return DocumentRange objects:

Show 64 links

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

View Example

Document document = wordProcessor.Document;
document.AppendText("ABCDEFGH");

DocumentRange r1 = document.CreateRange(1, 3);
DocumentPosition pos1 = document.CreatePosition(2);

DocumentRange r2 = document.InsertText(pos1, ">>NewText<<");

string s1 = String.Format("Range r1 starts at {0}, ends at {1}", r1.Start, r1.End);
string s2 = 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.

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.

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);

Extension Methods

See Also