TextPointer.GetOffsetToPosition(TextPointer) Methode

Definitie

Retourneert het aantal symbolen tussen de huidige TextPointer en een tweede opgegeven 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

Parameters

position
TextPointer

Een TextPointer die een positie aangeeft waarop de afstand (in symbolen) moet worden gevonden.

Retouren

Het relatieve aantal symbolen tussen de huidige TextPointer en position. Een negatieve waarde geeft aan dat de huidige TextPointer de positie volgt die is opgegeven door position, 0 geeft aan dat de posities gelijk zijn en een positieve waarde geeft aan dat de huidige TextPointer voorafgaat aan de positie die is opgegeven door position.

Uitzonderingen

position geeft een positie buiten de tekstcontainer die aan de huidige positie is gekoppeld.

Voorbeelden

In het volgende voorbeeld ziet u een gebruik voor deze methode. In het voorbeeld wordt de GetOffsetToPosition methode gebruikt om de offsets voor twee TextPointer exemplaren te vinden en gebruikt u deze informatie vervolgens om de selectie in een RichTextBoxop te slaan en te herstellen. In het voorbeeld wordt ervan uitgegaan dat de inhoud van de RichTextBox selectie niet is gewijzigd tussen het opslaan van een selectie en het herstellen van een selectie.

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

Opmerkingen

Een van de volgende wordt beschouwd als een symbool:

  • Een tag voor openen of sluiten voor een TextElement element.

  • Een UIElement element in een InlineUIContainer of BlockUIContainer. Houd er rekening mee dat een UIElement dergelijk symbool altijd wordt geteld als precies één symbool; eventuele extra inhoud of elementen die door de UIElement elementen zijn opgenomen, worden niet meegeteld als symbolen.

  • Een 16-bits Unicode-teken in een tekstelement Run .

Van toepassing op

Zie ook