TextPointer.GetNextInsertionPosition Method

Returns a TextPointer to the next insertion position in the specified logical direction.

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

Public Function GetNextInsertionPosition ( _
	direction As LogicalDirection _
) As TextPointer


Type: System.Windows.Documents.LogicalDirection
One of the LogicalDirection values that specify the logical direction in which to search for the next insertion position.

Return Value

Type: System.Windows.Documents.TextPointer
A TextPointer that identifies the next insertion position in the requested direction, or Nothing if no next insertion position can be found.

An insertion position is 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).

The following code uses the GetNextInsertionPosition method to underline the last word in the RichTextBox. This code example is part of a larger example used in the TextPointer class.

Run this sample

    '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
        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)
            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


Supported in: 5, 4

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

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

Community Additions