TextPointer.GetOffsetToPosition(TextPointer) Metode

Definisi

Mengembalikan jumlah simbol antara saat ini TextPointer dan kedua yang ditentukan 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

Parameter

position
TextPointer

TextPointer yang menentukan posisi untuk menemukan jarak (dalam simbol) ke.

Mengembalikan

Jumlah relatif simbol antara saat ini TextPointer dan position. Nilai negatif menunjukkan bahwa saat ini TextPointer mengikuti posisi yang ditentukan oleh position, 0 menunjukkan bahwa posisi sama, dan nilai positif menunjukkan bahwa saat ini TextPointer mendahului posisi yang ditentukan oleh position.

Pengecualian

position menentukan posisi di luar kontainer teks yang terkait dengan posisi saat ini.

Contoh

Contoh berikut menunjukkan penggunaan untuk metode ini. Contoh GetOffsetToPosition menggunakan metode untuk menemukan offset untuk dua TextPointer instans, lalu menggunakan informasi ini untuk menyimpan dan memulihkan pilihan dalam RichTextBox. Contoh mengasumsikan RichTextBox bahwa konten tidak berubah antara penyimpanan pilihan dan pemulihan pilihan.

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

Keterangan

Salah satu hal berikut dianggap sebagai simbol:

Berlaku untuk

Lihat juga