TextPointer.GetOffsetToPosition(TextPointer) Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
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:
Tag pembuka atau penutup untuk TextElement elemen.
UIElement Elemen yang terkandung dalam InlineUIContainer atau BlockUIContainer. Perhatikan bahwa seperti UIElement itu selalu dihitung sebagai tepat satu simbol; konten atau elemen tambahan yang terkandung oleh UIElement tidak dihitung sebagai simbol.
Karakter Unicode 16-bit di dalam elemen teks Run .