Поделиться через


TextPointer.CompareTo Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Function CompareTo ( _
    position As TextPointer _
) As Integer
public int CompareTo(
    TextPointer position
)

Parameters

Return Value

Type: System.Int32
-1 if the current TextPointer precedes position; 0 if the locations are the same; +1 if the current TextPointer follows positions.

Exceptions

Exception Condition
ArgumentException

position specifies a position from a different RichTextBox associated with the current position.

Examples

The following code uses the CompareTo method to find out if the RichTextBox is empty. This code example is part of a larger example used in the TextPointer class.

    'This method returns true if the RichTextBox is empty.
    Public Function isRichTextBoxEmpty() As Boolean
        Dim startPointer As TextPointer = MyRTB1.ContentStart.GetNextInsertionPosition(LogicalDirection.Forward)
        Dim endPointer As TextPointer = MyRTB1.ContentEnd.GetNextInsertionPosition(LogicalDirection.Backward)
        If (startPointer.CompareTo(endPointer) = 0) Then
            Return True
        Else
            Return False
        End If
    End Function

//This method returns true if the RichTextBox is empty.
public bool isRichTextBoxEmpty()
{
    TextPointer startPointer = MyRTB1.ContentStart.GetNextInsertionPosition(LogicalDirection.Forward);
    TextPointer endPointer = MyRTB1.ContentEnd.GetNextInsertionPosition(LogicalDirection.Backward);
    if (startPointer.CompareTo(endPointer) == 0)
        return true;
    else
        return false;
}

Version Information

Silverlight

Supported in: 5, 4

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.