TextPointer.IsInSameDocument Method

Determines whether or not a specified position is in the same text container as the current position.

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

public bool IsInSameDocument (
	TextPointer textPosition
public boolean IsInSameDocument (
	TextPointer textPosition
public function IsInSameDocument (
	textPosition : TextPointer
) : boolean
You cannot use methods in XAML.



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

Return Value

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

Exception typeCondition


Raised when textPosition is null.

Most operations that involve multiple TextPointers are only valid if the TextPointers in question indicate positions that are in the same text container scope. For example the CompareTo and GetOffsetToPosition methods cannot be used with TextPointers 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.

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions