Aracılığıyla paylaş


TextPointer.GetOffsetToPosition(TextPointer) Yöntem

Tanım

Geçerli TextPointer ile belirtilen TextPointersaniye arasındaki sembol sayısını döndürür.

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

Parametreler

position
TextPointer

Uzaklığı TextPointer (sembollerde) bulmak için bir konum belirten bir.

Döndürülenler

Int32

Geçerli TextPointer ve positionarasındaki göreli simge sayısı. Negatif bir değer, geçerlinin TextPointer tarafından positionbelirtilen konumu izlediğini gösterir, 0 konumların eşit olduğunu, pozitif bir değer ise geçerlinin TextPointer tarafından positionbelirtilen konumdan önce geldiğini gösterir.

Özel durumlar

position geçerli konumla ilişkili metin kapsayıcısının dışında bir konum belirtir.

Örnekler

Aşağıdaki örnekte bu yöntem için bir kullanım gösterilmektedir. Örnek, iki TextPointer örneğin uzaklıklarını bulmak için yöntemini kullanır GetOffsetToPosition ve ardından bir içindeki RichTextBoxseçimi kaydedip geri yüklemek için bu bilgileri kullanır. Örnekte, öğesinin içeriğinin RichTextBox seçim kaydetme ve seçim geri yükleme arasında değişmediği varsayılır.

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

Açıklamalar

Aşağıdakilerden herhangi biri simge olarak kabul edilir:

Şunlara uygulanır

Ayrıca bkz.