Language

TextPointer.GetOffsetToPosition(TextPointer) メソッド

定義

現在の TextPointer と指定した 2 番目の TextPointerの間のシンボルの数を返します。

public:
 int GetOffsetToPosition(System::Windows::Documents::TextPointer ^ position);
public int GetOffsetToPosition(System.Windows.Documents.TextPointer position);
member this.GetOffsetToPosition : System.Windows.Documents.TextPointer -> int
Public Function GetOffsetToPosition (position As TextPointer) As Integer

パラメーター

position
TextPointer

距離を (シンボル単位で) 検索する位置を指定する TextPointer 。

返品

現在の TextPointer と positionの間のシンボルの相対数。 負の値は、現在の TextPointer が positionで指定された位置に従っていることを示し、0 は位置が等しいことを示し、正の値は現在の TextPointer が positionで指定された位置の前にあることを示します。

例外

position は、現在の位置に関連付けられているテキスト コンテナーの外側の位置を指定します。

例

次の例では、このメソッドの使用方法を示します。 この例では、 GetOffsetToPosition メソッドを使用して 2 つの TextPointer インスタンスのオフセットを検索し、この情報を使用して RichTextBox内の選択範囲を保存および復元します。 この例では、選択保存と選択復元の間で RichTextBox の内容が変更されていないことを前提としています。

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.
    richTextBox.Selection.Select(
       contentStart.GetPositionAtOffset(selectionOffsets.Start),
       contentStart.GetPositionAtOffset(selectionOffsets.End)
    );
}
Private Structure SelectionOffsets
    Friend Start As Integer
    Friend [End] As Integer
End Structure

Private Function GetSelectionOffsetsRTB(ByVal richTextBox As RichTextBox) As SelectionOffsets
    Dim selectionOffsets As SelectionOffsets

    Dim contentStart As TextPointer = 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
End Function

Private Sub RestoreSelectionOffsetsRTB(ByVal richTextBox As RichTextBox, ByVal selectionOffsets As SelectionOffsets)
    Dim contentStart As TextPointer = richTextBox.Document.ContentStart

    ' Use previously determined offsets to create corresponding TextPointers,
    ' and use these to restore the selection.
    richTextBox.Selection.Select(contentStart.GetPositionAtOffset(selectionOffsets.Start), contentStart.GetPositionAtOffset(selectionOffsets.End))
End Sub

注釈

次のいずれかがシンボルと見なされます。

  • TextElement要素の開始タグまたは終了タグ。

  • UIElementまたはInlineUIContainerに含まれるBlockUIContainer要素。 このような UIElement は常に 1 つのシンボルとしてカウントされます。 UIElement に含まれる追加のコンテンツまたは要素はシンボルとしてカウントされません。

  • テキスト Run 要素内の 16 ビット Unicode 文字。

適用対象

こちらもご覧ください