TextPointer.GetOffsetToPosition(TextPointer) TextPointer.GetOffsetToPosition(TextPointer) TextPointer.GetOffsetToPosition(TextPointer) TextPointer.GetOffsetToPosition(TextPointer) Method

Definición

Devuelve el recuento de símbolos entre el TextPointer actual y un segundo TextPointer especificado.Returns the count of symbols between the current TextPointer and a second specified 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

Parámetros

position
TextPointer TextPointer TextPointer TextPointer

TextPointer que especifica una posición para buscar la distancia hasta ella (en símbolos).A TextPointer that specifies a position to find the distance (in symbols) to.

Devoluciones

El número relativo de símbolos entre el TextPointer actual y position.The relative number of symbols between the current TextPointer and position. Un valor negativo indica que el TextPointer actual sigue a la posición especificada por position, 0 indica que las posiciones son iguales y un valor positivo, que el TextPointer actual precede a la posición especificada por position.A negative value indicates that the current TextPointer follows the position specified by position, 0 indicates that the positions are equal, and a positive value indicates that the current TextPointer precedes the position specified by position.

Excepciones

position especifica una posición fuera del contenedor de texto asociado a la posición actual.position specifies a position outside of the text container associated with the current position.

Ejemplos

En el ejemplo siguiente se muestra un uso para este método.The following example demonstrates a use for this method. En el ejemplo se GetOffsetToPosition utiliza el método para buscar los desplazamientos de dos TextPointer instancias y, a continuación, se usa esta información para guardar RichTextBoxy restaurar la selección en.The example uses the GetOffsetToPosition method to find the offsets for two TextPointer instances, and then uses this information to save and restore the selection in a RichTextBox. En el ejemplo se da por supuesto que RichTextBox el contenido de no ha cambiado entre una selección guardar y una restauración de selección.The example assumes that the contents of the RichTextBox have not changed between a selection save and a selection restore.

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 elementos se considera un símbolo:Any of the following is considered to be a symbol:

Se aplica a

Consulte también: