Edit

Share via


TextRange(TextPointer, TextPointer) Constructor

Definition

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

public:
 TextRange(System::Windows::Documents::TextPointer ^ position1, System::Windows::Documents::TextPointer ^ position2);
public TextRange (System.Windows.Documents.TextPointer position1, System.Windows.Documents.TextPointer position2);
new System.Windows.Documents.TextRange : System.Windows.Documents.TextPointer * System.Windows.Documents.TextPointer -> System.Windows.Documents.TextRange
Public Sub New (position1 As TextPointer, position2 As TextPointer)

Parameters

position1
TextPointer

A fixed anchor position that marks one end of the selection used to form the new TextRange.

position2
TextPointer

A movable position that marks the other end of the selection used to form the new TextRange.

Exceptions

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

Occurs when position1 or position2 is null.

Examples

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;
}
' This method returns a plain text representation of a specified FlowDocument.
Private Function GetTextFromFlowDocument(ByVal flowDoc As FlowDocument) As String
    ' Create a new TextRanage that takes the entire FlowDocument as the current selection.
    Dim flowDocSelection As 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
End Function

Remarks

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.

Applies to