TextPointer.IsInSameDocument(TextPointer) Method

Definition

Indicates whether the specified position is in the same text container as the current position.

public:
 bool IsInSameDocument(System::Windows::Documents::TextPointer ^ textPosition);
public bool IsInSameDocument (System.Windows.Documents.TextPointer textPosition);
member this.IsInSameDocument : System.Windows.Documents.TextPointer -> bool
Public Function IsInSameDocument (textPosition As TextPointer) As Boolean

Parameters

textPosition
TextPointer

A TextPointer that specifies a position to compare to the current position.

Returns

true if textPosition indicates a position that is in the same text container as the current position; otherwise, false.

Exceptions

textPosition is null.

Examples

The following example demonstrates a use for this method. The example uses the IsInSameDocument method to check whether a specified TextPointer is positioned between two other specified TextPointer instances in a situation when there is no guarantee that all three positions belong to the same text container.

// This method first checks for compatible text container scope, and then checks whether
// a specified position is between two other specified positions.
bool IsPositionContainedBetween(TextPointer positionToTest, TextPointer start, TextPointer end)
{
    // Note that without this check, an exception will be raised by CompareTo if positionToTest 
    // does not point to a position that is in the same text container used by start and end.
    //
    // This test also implicitely indicates whether start and end share a common text container.
    if (!positionToTest.IsInSameDocument(start) || !positionToTest.IsInSameDocument(end)) 
        return false;
    
    return start.CompareTo(positionToTest) <= 0 && positionToTest.CompareTo(end) <= 0;
}
' This method first checks for compatible text container scope, and then checks whether
' a specified position is between two other specified positions.
Private Function IsPositionContainedBetween(ByVal positionToTest As TextPointer, ByVal start As TextPointer, ByVal [end] As TextPointer) As Boolean
    ' Note that without this check, an exception will be raised by CompareTo if positionToTest 
    ' does not point to a position that is in the same text container used by start and end.
    '
    ' This test also implicitely indicates whether start and end share a common text container.
    If (Not positionToTest.IsInSameDocument(start)) OrElse (Not positionToTest.IsInSameDocument([end])) Then
        Return False
    End If

    Return start.CompareTo(positionToTest) <= 0 AndAlso positionToTest.CompareTo([end]) <= 0
End Function

Remarks

Most operations that involve multiple TextPointer instances are only valid if the instances in question indicate positions that are in the same text container scope. For example the CompareTo and GetOffsetToPosition methods cannot be used with a TextPointer to a position outside of the text container associated with the current position. Use this method to verify that a specified TextPointer is compatible with the current position for such operations.

Applies to