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 Function GetPositionAtOffset ( _
	offset As Integer, _
	direction As LogicalDirection _
) As TextPointer


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 Nothing 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 Sub MyButton2_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    MyButton2.IsTabStop = False
    'Create two arbitrary TextPointers to specify the range of content to select.
    Dim MyTP1 As TextPointer = MyPg.ContentStart.GetPositionAtOffset(2, LogicalDirection.Forward)
    Dim MyTP2 As TextPointer = MyPg.ContentEnd.GetPositionAtOffset(-1, LogicalDirection.Backward)

    'Programmatically change the selection in the RichTextBox
    MyRTB2.Selection.[Select](MyTP1, MyTP2)
    Dim s As String = MyRTB2.Selection.Text
End Sub


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