TextPointer.CompareTo(TextPointer) Method

Definition

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

C#
public int CompareTo(System.Windows.Documents.TextPointer position);

Parameters

position
TextPointer

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

Returns

-1 if the current TextPointer precedes position; 0 if the locations are the same; +1 if the current TextPointer follows position.

Exceptions

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

Examples

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 a specified TextElement is empty.

C#
// 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.

Remarks

A value of -1 indicates that the position specified by the current TextPointer precedes the position specified by position. A value of 0 indicates that the indicated positions are equal. A value of positive +1 indicates that the position specified by the current TextPointer follows the position specified by position.

Applies to

Produkt Verze
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also