This documentation is archived and is not being maintained.

TextRange Constructor

Initializes a new instance of the TextRange class, taking two specified TextPointer positions as the beginning and end positions for the new range.

Namespace:  System.Windows.Documents
Assembly:  PresentationFramework (in PresentationFramework.dll)

public TextRange(
	TextPointer position1,
	TextPointer position2


Type: System.Windows.Documents.TextPointer
A fixed anchor position that marks one end of the selection used to form the new TextRange.
Type: System.Windows.Documents.TextPointer
A movable position that marks the other end of the selection used to form the new TextRange.


Occurs when position1 and position2 are not positioned within the same document.


Occurs when position1 or position2 is null.

A TextRange is formed from a selection between two positions indicated by TextPointers. One of these positions (indicated by position1) is fixed with respect to the selection, while the other position (indicated by position2) is movable. This is similar to how a selection made by a user using the mouse or keyboard behaves.

The actual ends of the new TextRange may be adjusted to match any selection heuristics that are applicable to the document that contains the new TextRange.

The following example demonstrates the use of the TextRange constructor.

// This method returns a plain text representation of a specified FlowDocument.
string GetTextFromFlowDocument(FlowDocument flowDoc)
     // Create a new TextRanage that takes the entire FlowDocument as the current selection.
     TextRange flowDocSelection = new TextRange(flowDoc.ContentStart, flowDoc.ContentEnd);

     // Use the Text property to extract a string that contains the unformatted text contents 
     // of the FlowDocument.
     return flowDocSelection.Text;

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.