TextPointer.GetOffsetToPosition(TextPointer) Metódus

Definíció

Az aktuális TextPointer és a megadott TextPointermásodperc közötti szimbólumok számát adja vissza.

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

Paraméterek

position
TextPointer

Az TextPointer a hely, amellyel megtalálhatja a távolságot (szimbólumokban).

Válaszok

Az aktuális TextPointer és positiona . A negatív érték azt jelzi, hogy az áram TextPointer a megadott pozíciót positionköveti, a 0 azt jelzi, hogy a pozíciók egyenlőek, a pozitív érték pedig azt jelzi, hogy az áram TextPointer megelőzi a megadott pozíciót position.

Kivételek

position az aktuális pozícióhoz társított szövegtárolón kívüli pozíciót határoz meg.

Példák

Az alábbi példa a metódus használatát mutatja be. A példa a GetOffsetToPosition metódussal megkeresi a két TextPointer példány eltolásait, majd ezeket az információkat felhasználva menti és visszaállítja a kijelölést egy RichTextBoxadott példányban. A példa feltételezi, hogy a kijelölés mentése és a RichTextBox kijelölés visszaállítása között nem változott a tartalom.

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

Megjegyzések

Az alábbiak bármelyike szimbólumnak minősül:

  • Elem nyitó vagy záró címkéje TextElement .

  • Egy UIElement vagy InlineUIContainertöbb elemet tartalmazó BlockUIContainer elem. Vegye figyelembe, hogy az UIElement ilyen elemeket mindig pontosan egy szimbólumnak számítják; a UIElement bennük található további tartalmak vagy elemek nem számítanak szimbólumnak.

  • 16 bites Unicode-karakter egy szövegelemen Run belül.

A következőre érvényes:

Lásd még