TextPointer.GetPositionAtOffset Method

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.

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

public TextPointer GetPositionAtOffset(
	int offset,
	LogicalDirection direction


Type: System.Int32
An offset, in symbols, for which to calculate and return the position. If the offset is negative, the returned TextPointer precedes the current TextPointer; otherwise, it follows.
Type: System.Windows.Documents.LogicalDirection
One of the LogicalDirection values that specifies the logical direction of the returned TextPointer.

Return Value

Type: System.Windows.Documents.TextPointer
A TextPointer to the position indicated by the specified offset and in the direction specified by the direction parameter, or null if the offset extends past the end of the content.

Any of the following is considered to be a symbol:

  • An opening or closing tag for the TextElement element.

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

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

The following code uses the GetPositionAtOffset method to select arbitrary text in a RichTextBox.

Run this sample

<RichTextBox Name="MyRTB2" Width="150" Height="30" Margin="5">
    <Paragraph x:Name="MyPg">Arbitrary text selection</Paragraph>    
<Button Click="MyButton2_Click" Content="Select some text" Height="23" Name="MyButton2" Width="112" />

private void MyButton2_Click(object sender, RoutedEventArgs e)
    MyButton2.IsTabStop = false;
    //Create two arbitrary TextPointers to specify the range of content to select.
    TextPointer MyTP1 = MyPg.ContentStart.GetPositionAtOffset(2, LogicalDirection.Forward);
    TextPointer MyTP2 = MyPg.ContentEnd.GetPositionAtOffset(-1, LogicalDirection.Backward);

    //Programmatically change the selection in the RichTextBox
    MyRTB2.Selection.Select(MyTP1, MyTP2);


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