Compartir a través de


TextPointer Class

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

Represents a position within a RichTextBox.

Inheritance Hierarchy

System.Object
  System.Windows.Documents.TextPointer

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

Syntax

'Declaration
Public Class TextPointer
public class TextPointer

The TextPointer type exposes the following members.

Properties

  Name Description
Public propertySupported by Silverlight for Windows Phone IsAtInsertionPosition Gets a value that indicates whether the current position is an insertion.
Public propertySupported by Silverlight for Windows Phone LogicalDirection Gets the logical direction associated with the current position, which is used to disambiguate content associated with the current position.
Public propertySupported by Silverlight for Windows Phone Parent Gets the logical parent that contains the current position.
Public property VisualParent Gets the visual tree parent of the TextPointer object.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows Phone CompareTo Performs an ordinal comparison between the positions specified by the current TextPointer and a second specified TextPointer.
Public methodSupported by Silverlight for Windows Phone Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows Phone Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetCharacterRect Returns a bounding box for content that borders the current TextPointer in the specified logical direction.
Public methodSupported by Silverlight for Windows Phone GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetNextInsertionPosition Returns a TextPointer to the next insertion position in the specified logical direction.
Public methodSupported by Silverlight for Windows Phone GetPositionAtOffset Returns a TextPointer to the position indicated by the specified offset, in symbols, from the beginning of the current TextPointer and in the specified direction.
Public methodSupported by Silverlight for Windows Phone GetType Gets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows Phone MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

The TextPointer class is intended to facilitate traversal and manipulation of content in a RichTextBox. The TextPointer class introduces the following terminology:

  • Position - A TextPointer that points to a location in the content of a RichTextBox. A position either occurs between characters in the content, or between content element tags that define structure for the content.

  • Current Position - The position indicated by the current TextPointer. Many of the operations that can be performed with a TextPointer are relative to the current TextPointer position. Therefore, a position indicated by a TextPointer is typically referred to as the current position.

  • Insertion Position - A position where new content may be added without breaking any semantic rules for the associated content. In practice, an insertion position is anywhere in content where a caret may be positioned. An example of a valid TextPointer position that is not an insertion position is the position between two adjacent Paragraph tags (that is, between the closing tag of the preceding paragraph and the opening tag of the next paragraph).

  • Symbol - For TextPointer operations, any of the following is considered to be a symbol:

    • An opening or closing tag for a TextElement element.

    • A UIElement element contained within an InlineUIContainer. Note that a UIElement is always counted as exactly one symbol. Any additional content or elements contained by the UIElement are not considered symbols.

    • Each 16-bit Unicode character inside of a text Run element.

  • Text Container - The element that forms the border for the content. The position indicated by a TextPointer always occurs within a text container. Currently, a text container can only be a RichTextBox. Typically, operations between TextPointer instances in different text containers are not supported.

Some of the operations that TextPointer facilitates include the following:

  • Performing an ordinal comparison of the current position with a second specified position. For more information, see the CompareTo method.

  • Get the TextElement or the RichTextBox that scopes the specified position. For more information, see the Parent property.

  • Translating between TextPointer positions and symbol offsets into content. For more information, see the GetPositionAtOffset method.

  • Performing visual hit testing by translating between a TextPointer position and a Point representing relative coordinates.

  • Finding a nearby insertion position, or checking whether the current position is an insertion position. For more information, see the GetNextInsertionPosition method and the IsAtInsertionPosition property.

The position and LogicalDirection indicated by a TextPointer object are immutable. When content is edited or modified, the position indicated by a TextPointer does not change relative to the surrounding text. Instead, the offset of that position from the beginning of content is adjusted correspondingly to reflect the new relative position in content. For example, a TextPointer that indicates a position at the beginning of a paragraph continues to point to the beginning of that paragraph even when content is inserted or deleted before or after the paragraph.

The TextPointer class does not provide any public constructors. An instance of TextPointer is created by using properties or methods of other objects (including other TextPointer objects). The following list provides a few examples of methods and properties that create and return a TextPointer. This list is not exhaustive:

Examples

The following code example highlights the first word and underlines the last word in the sentence that you type in the RichTextBox. In this example, a space character is used as the word boundary. The example will also identify if the RichTextBox is empty.

Run this sample

<Canvas x:Name="LayoutRoot" Background="White">
    <RichTextBox x:Name="MyRTB1" Width="301" Canvas.Left="0" Canvas.Top="41" />

    <Button x:Name="MyButton" Content="Click here" Click="MyButton_Click" Canvas.Left="0" Canvas.Top="71" />
    <TextBlock x:Name="MyTBl" Canvas.Left="83" Canvas.Top="71" Height="23"  Width="218" />
    <TextBlock Canvas.Left="0" Canvas.Top="12" Height="23" Name="textBlock1" Text="Enter a sentence." Width="225" />
</Canvas>
'This method underlines the last word in a RichTextBox
Public Sub UnderlineLastWord()
    Dim EndofContent As TextPointer = MyRTB1.ContentEnd.GetNextInsertionPosition(LogicalDirection.Backward)
    Dim currentPointer As TextPointer = EndofContent.GetNextInsertionPosition(LogicalDirection.Backward)
    If (currentPointer Is Nothing) Then
        Return
    End If
    Dim currentChar As String = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Backward)

    While ((currentChar <> " ") _
                AndAlso (currentChar <> ""))
        currentPointer = currentPointer.GetNextInsertionPosition(LogicalDirection.Backward)
        currentChar = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Backward)

    End While
    If (currentChar = " ") Then
        MyRTB1.Selection.Select(currentPointer.GetNextInsertionPosition(LogicalDirection.Forward), EndofContent)
    Else
        MyRTB1.Selection.Select(currentPointer, EndofContent)
    End If
    MyRTB1.Selection.ApplyPropertyValue(Run.TextDecorationsProperty, TextDecorations.Underline)
End Sub

Private Function GetCurrentChar(ByVal RTB As RichTextBox, ByVal pointer As TextPointer, ByVal direction As LogicalDirection) As String
    Dim nextPointer As TextPointer = pointer.GetNextInsertionPosition(direction)
    If (Not (nextPointer) Is Nothing) Then
        RTB.Selection.Select(pointer, nextPointer)
        If (RTB.Selection.Text.Length <> 0) Then
            Return RTB.Selection.Text(0).ToString
        End If
    End If
    Return ""
End Function

'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 highlights the first word in the RichTextBox
Public Sub HighlightFirstWord()
    Dim StartofContent As TextPointer = MyRTB1.ContentStart
    Dim currentPointer As TextPointer = StartofContent.GetNextInsertionPosition(LogicalDirection.Forward)
    If (currentPointer Is Nothing) Then
        Return
    End If
    Dim currentChar As String = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Forward)

    While ((currentChar <> " ") _
                AndAlso (currentChar <> ""))
        currentPointer = currentPointer.GetNextInsertionPosition(LogicalDirection.Forward)
        currentChar = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Forward)

    End While
    Dim StartRect As Rect = StartofContent.GetCharacterRect(LogicalDirection.Forward)
    Dim EndRect As Rect = currentPointer.GetCharacterRect(LogicalDirection.Forward)
    StartRect.Union(EndRect)
    CreateHighlightRectangle(StartRect)
End Sub

Private Function CreateHighlightRectangle(ByVal bounds As Rect) As Rectangle
    Dim r As Rectangle = New Rectangle
    r.Fill = New SolidColorBrush(Color.FromArgb(75, 0, 0, 200))
    r.Stroke = New SolidColorBrush(Color.FromArgb(230, 0, 0, 254))
    r.StrokeThickness = 1
    r.Width = bounds.Width
    r.Height = bounds.Height
    Canvas.SetLeft(r, bounds.Left)
    Canvas.SetTop(r, (bounds.Top + 41))
    LayoutRoot.Children.Add(r)
    Return r
End Function

Private Sub MyButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    If isRichTextBoxEmpty Then
        MyTBl.Text = "RichTextBox is empty"
    Else
        HighlightFirstWord()
        UnderlineLastWord()
        MyTBl.Text = ""
    End If
End Sub
//This method underlines the last word in a RichTextBox
public void UnderlineLastWord()
{
    TextPointer EndofContent = MyRTB1.ContentEnd.GetNextInsertionPosition(LogicalDirection.Backward);
    TextPointer currentPointer = EndofContent.GetNextInsertionPosition(LogicalDirection.Backward);

    if (currentPointer == null)
        return;

    string currentChar = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Backward);

    while (currentChar != " " && currentChar != "")
    {
        currentPointer = currentPointer.GetNextInsertionPosition(LogicalDirection.Backward);
        currentChar = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Backward);
    }

    if (currentChar == " ")
        MyRTB1.Selection.Select(currentPointer.GetNextInsertionPosition(LogicalDirection.Forward), EndofContent);
    else
        MyRTB1.Selection.Select(currentPointer, EndofContent);

    MyRTB1.Selection.ApplyPropertyValue(Run.TextDecorationsProperty, TextDecorations.Underline);
}

private string GetCurrentChar(RichTextBox RTB, TextPointer pointer, LogicalDirection direction)
{
    TextPointer nextPointer = pointer.GetNextInsertionPosition(direction);
    if (nextPointer != null)
    {
        RTB.Selection.Select(pointer, nextPointer);
        if (RTB.Selection.Text.Length != 0)
            return RTB.Selection.Text[0].ToString();
    }
    return "";
}

//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;
}

//This method highlights the first word in the RichTextBox
public void HighlightFirstWord()
{
    TextPointer StartofContent = MyRTB1.ContentStart;
    TextPointer currentPointer = StartofContent.GetNextInsertionPosition(LogicalDirection.Forward);

    if (currentPointer == null)
        return;

    string currentChar = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Forward);

    while (currentChar != " " && currentChar != "")
    {
        currentPointer = currentPointer.GetNextInsertionPosition(LogicalDirection.Forward);
        currentChar = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Forward);
    }

    Rect StartRect = StartofContent.GetCharacterRect(LogicalDirection.Forward);
    Rect EndRect = currentPointer.GetCharacterRect(LogicalDirection.Forward);
    StartRect.Union(EndRect);
    CreateHighlightRectangle(StartRect);

}

private Rectangle CreateHighlightRectangle(Rect bounds)
{
    Rectangle r = new Rectangle();
    r.Fill = new SolidColorBrush(Color.FromArgb(75, 0, 0, 200));
    r.Stroke = new SolidColorBrush(Color.FromArgb(230, 0, 0, 254));
    r.StrokeThickness = 1;
    r.Width = bounds.Width;
    r.Height = bounds.Height;
    Canvas.SetLeft(r, bounds.Left);
    Canvas.SetTop(r, bounds.Top + 41);

    LayoutRoot.Children.Add(r);

    return r;
}

private void MyButton_Click(object sender, RoutedEventArgs e)
{
    if (isRichTextBoxEmpty())
        MyTBl.Text = "RichTextBox is empty";
    else
    {
        HighlightFirstWord();
        UnderlineLastWord();
        MyTBl.Text = "";
    }

}

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.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.