TextPointer.GetOffsetToPosition(TextPointer) Método
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Devuelve el recuento de símbolos entre el TextPointer actual y un segundo TextPointerespecificado.
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
Parámetros
- position
- TextPointer
TextPointer que especifica una posición a la que se va a buscar la distancia (en símbolos).
Devoluciones
Número relativo de símbolos entre el actual TextPointer y position. Un valor negativo indica que el actual TextPointer sigue la posición especificada por position, 0 indica que las posiciones son iguales y un valor positivo indica que el actual TextPointer precede a la posición especificada por position.
Excepciones
position especifica una posición fuera del contenedor de texto asociado a la posición actual.
Ejemplos
En el ejemplo siguiente se muestra un uso para este método. En el ejemplo se usa el GetOffsetToPosition método para buscar los desplazamientos de dos TextPointer instancias y, a continuación, se usa esta información para guardar y restaurar la selección en .RichTextBox En el ejemplo se supone que el contenido de no RichTextBox ha cambiado entre un guardado de selección y una restauración de selección.
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
Comentarios
Cualquiera de los siguientes se considera un símbolo:
Etiqueta de apertura o cierre para un elemento TextElement.
Elemento UIElement contenido en o InlineUIContainerBlockUIContainer. Tenga en cuenta que tal UIElement siempre se cuenta como un símbolo exactamente; ningún contenido o elemento adicional contenido de la UIElement no se cuentan como símbolos.
Carácter Unicode de 16 bits dentro de un elemento de texto Run .