TextPointer.GetOffsetToPosition(TextPointer) Metodo

Definizione

Restituisce il conteggio dei simboli tra l'oggetto TextPointer corrente e un secondo oggetto TextPointer specificato.

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

Parametri

position
TextPointer

Oggetto TextPointer che specifica una posizione dalla quale cercare la distanza (in simboli).

Restituisce

Int32

Numero di simboli relativo tra l'oggetto TextPointer corrente e position. Un valore negativo indica che l'oggetto TextPointer corrente segue la posizione specificata da position; 0 indica che le posizioni sono uguali; un valore positivo indica che l'oggetto TextPointer corrente precede la posizione specificata da position.

Eccezioni

position specifica una posizione esterna al contenitore di testo associato alla posizione corrente.

Esempio

Nell'esempio seguente viene illustrato un utilizzo per questo metodo. Nell'esempio viene utilizzato il GetOffsetToPosition metodo per trovare gli offset per due TextPointer istanze e quindi vengono utilizzate queste informazioni per salvare e ripristinare la selezione in un oggetto RichTextBox. Nell'esempio si presuppone che il contenuto di non sia stato modificato tra un salvataggio di RichTextBox selezione e un ripristino di selezione.

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

Commenti

Uno dei seguenti è considerato un simbolo:

  • Tag di apertura o chiusura per un TextElement elemento.

  • Elemento UIElement contenuto in un InlineUIContainer oggetto o BlockUIContainer. Si noti che tale UIElement oggetto viene sempre conteggiato come esattamente un simbolo. Qualsiasi contenuto o elemento aggiuntivo contenuto contenuto da UIElement non viene conteggiato come simboli.

  • Carattere Unicode a 16 bit all'interno di un elemento di testo Run .

Si applica a

Vedi anche