This documentation is archived and is not being maintained.

TextPointer.GetOffsetToPosition Method

Returns the count of symbols between the current TextPointer and a second specified TextPointer.

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

public int GetOffsetToPosition(
	TextPointer position


Type: System.Windows.Documents.TextPointer
A TextPointer that specifies a position to find the distance (in symbols) to.

Return Value

Type: System.Int32
The relative number of symbols between the current TextPointer and position. A negative value indicates that the current TextPointer follows the position specified by position, 0 indicates that the positions are equal, and a positive value indicates that the current TextPointer precedes the position specified by position.


position specifies a position outside of the text container associated with the current position.

Any of the following is considered to be a symbol:

The following example demonstrates a use for this method. The example uses the GetOffsetToPosition method to find the offsets for two TextPointer instances, and then uses this information to save and restore the selection in a RichTextBox. The example assumes that the contents of the RichTextBox have not changed between a selection save and a selection restore.

struct SelectionOffsets { internal int Start; internal int End; }

SelectionOffsets GetSelectionOffsetsRTB(RichTextBox richTextBox)
    SelectionOffsets selectionOffsets;

    TextPointer contentStart = richTextBox.Document.ContentStart;

    // Find the offset for the starting and ending TextPointers.
    selectionOffsets.Start = contentStart.GetOffsetToPosition(richTextBox.Selection.Start);
    selectionOffsets.End = contentStart.GetOffsetToPosition(richTextBox.Selection.End);

    return selectionOffsets;

void RestoreSelectionOffsetsRTB(RichTextBox richTextBox, SelectionOffsets selectionOffsets)
    TextPointer contentStart = richTextBox.Document.ContentStart;

    // Use previously determined offsets to create corresponding TextPointers,
    // and use these to restore the selection.

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.