The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.
.NET Framework 4.5.2+
.NET Framework 4.5.2+
.NET Standard 2.0+

DocumentRange Interface

Defines a document range.

Namespace: DevExpress.XtraRichEdit.API.Native

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

Declaration

[ComVisible(true)]
public interface DocumentRange
<ComVisible(True)>
Public Interface DocumentRange
[ComVisible(true)]
public interface DocumentRange
<ComVisible(True)>
Public Interface DocumentRange

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

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.

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