TextPointer.GetOffsetToPosition(TextPointer) Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Vrátí počet symbolů mezi aktuálním TextPointer a druhým zadaným 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
Parametry
- position
- TextPointer
Určuje TextPointer pozici, do které se má najít vzdálenost (v symbolech).
Návraty
Relativní počet symbolů mezi aktuálním TextPointer a position. Záporná hodnota označuje, že aktuální TextPointer hodnota následuje za pozicí určenou positionhodnotou , 0 označuje, že pozice jsou rovny, a kladná hodnota označuje, že aktuální TextPointer předchází pozici určenou position.
Výjimky
position určuje pozici mimo textový kontejner přidružený k aktuální pozici.
Příklady
Následující příklad ukazuje použití pro tuto metodu. Příklad používá metodu GetOffsetToPosition k nalezení posunů pro dvě TextPointer instance a pak tyto informace používá k uložení a obnovení výběru v souboru RichTextBox. V příkladu se předpokládá, že se obsah RichTextBox výběru mezi uložením a obnovením výběru nezměnil.
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
Poznámky
Za symbol se považuje některý z následujících způsobů:
Levá nebo pravá značka pro prvek TextElement.
Prvek UIElement obsažený v objektu nebo InlineUIContainerBlockUIContainer. Všimněte si, že takový UIElement se vždy počítá jako přesně jeden symbol; žádný další obsah nebo prvky obsažené v UIElement se nezapočítávají jako symboly.
16bitový znak Unicode uvnitř textového Run prvku.