Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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 TextPointer GetNextInsertionPosition(
	LogicalDirection 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.TextPointer
A TextPointer that identifies the next insertion position in the requested direction, or null 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 void UnderlineLastWord()
    TextPointer EndofContent = MyRTB1.ContentEnd.GetNextInsertionPosition(LogicalDirection.Backward);
    TextPointer currentPointer = EndofContent.GetNextInsertionPosition(LogicalDirection.Backward);

    if (currentPointer == null)

    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);
        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 "";


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

© 2016 Microsoft