This documentation is archived and is not being maintained.

TextPointer.CompareTo Method

Performs an ordinal comparison between the positions specified by this TextPointer and a second specified TextPointer.

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

public int CompareTo (
	TextPointer position
public int CompareTo (
	TextPointer position
public function CompareTo (
	position : TextPointer
) : int
You cannot use methods in XAML.



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

Return Value

An integer specifying the ordinal relationship between this TextPointer and the TextPointer specified by position. A value of negative one (-1) indicates that the position specified by this TextPointer precedes the position specified by position. A value of zero (0) indicates that the indicated positions are equal. A value of positive one (+1) indicates that the position specified by this TextPointer follows the position specified by position.

Exception typeCondition


Raised when position specifies a position outside of the text container associated with the current position.

The following example demonstrates a use for this method. In the example, the CompareTo method is used in conjunction with the GetInsertionPosition method to test whether or not a specified TextElement is empty.

// Tests to see if the specified TextElement is empty (has no printatble content).
bool IsElementEmpty(TextElement element)
    // Find starting and ending insertion positions in the element.
    // Inward-facing directions are used to make sure that insertion position
    // will be found correctly in case when the element may contain inline 
    // formatting elements (such as a Span or Run that contains Bold or Italic elements).
    TextPointer start = element.ContentStart.GetInsertionPosition(LogicalDirection.Forward);
    TextPointer end = element.ContentEnd.GetInsertionPosition(LogicalDirection.Backward);
    // The element has no printable content if its first and last insertion positions are equal.
    return start.CompareTo(end) == 0;

} // End IsEmptyElement method.

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