TextPointer.GetNextInsertionPosition Method
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Returns a TextPointer to the next insertion position in the specified logical direction.
Assembly: System.Windows (in System.Windows.dll)
'Declaration Public Function GetNextInsertionPosition ( _ direction As LogicalDirection _ ) As TextPointer
Parameters
- direction
- 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.TextPointerA 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.
'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