Freigeben über


TextPointer.GetPositionAtOffset Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
Public Function GetPositionAtOffset ( _
    offset As Integer, _
    direction As LogicalDirection _
) As TextPointer
public TextPointer GetPositionAtOffset(
    int offset,
    LogicalDirection direction
)

Parameters

  • offset
    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.

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 nulla null reference (Nothing in Visual Basic) if the offset extends past the end of the content.

Remarks

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.

Examples

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>    
</RichTextBox>
<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
    MyRTB2.Focus()
End Sub
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);
    MyRTB2.Focus();
}

Version Information

Silverlight

Supported in: 5, 4

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

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