Redigeeri

Jagamisviis:


TextPointer.GetTextInRun Method

Definition

Returns text adjacent to the current TextPointer.

Overloads

GetTextInRun(LogicalDirection)

Returns a string containing any text adjacent to the current TextPointer in the specified logical direction.

GetTextInRun(LogicalDirection, Char[], Int32, Int32)

Copies the specified maximum number of characters from any adjacent text in the specified direction into a caller-supplied character array.

GetTextInRun(LogicalDirection)

Returns a string containing any text adjacent to the current TextPointer in the specified logical direction.

public:
 System::String ^ GetTextInRun(System::Windows::Documents::LogicalDirection direction);
public string GetTextInRun (System.Windows.Documents.LogicalDirection direction);
member this.GetTextInRun : System.Windows.Documents.LogicalDirection -> string
Public Function GetTextInRun (direction As LogicalDirection) As String

Parameters

direction
LogicalDirection

One of the LogicalDirection values that specifies the logical direction in which to find and return any adjacent text.

Returns

A string containing any adjacent text in the specified logical direction, or Empty if no adjacent text can be found.

Examples

The following example demonstrates a use for this method. The example uses the GetTextInRun method to implement a simple text extractor. The method returns a string concatenation of all text between two specified TextPointer instances.

While the example can be used to extract any text between two TextPointer instances, it is intended for illustrative purposes only, and should not be used in production code. Use the TextRange.Text property instead.

// Returns a string containing the text content between two specified TextPointers.
string GetTextBetweenTextPointers(TextPointer start, TextPointer end)
{
    StringBuilder buffer = new StringBuilder();
 
    while (start != null && start.CompareTo(end) < 0)
    {
        if (start.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
            buffer.Append(start.GetTextInRun(LogicalDirection.Forward));
 
        // Note that when the TextPointer points into a text run, this skips over the entire
        // run, not just the current character in the run.
        start = start.GetNextContextPosition(LogicalDirection.Forward);
    }
    return buffer.ToString();
} // End GetTextBetweenPointers.
' Returns a string containing the text content between two specified TextPointers.
Private Function GetTextBetweenTextPointers(ByVal start As TextPointer, ByVal [end] As TextPointer) As String
    Dim buffer As New StringBuilder()

    Do While start IsNot Nothing AndAlso start.CompareTo([end]) < 0
        If start.GetPointerContext(LogicalDirection.Forward) = TextPointerContext.Text Then
            buffer.Append(start.GetTextInRun(LogicalDirection.Forward))
        End If

        ' Note that when the TextPointer points into a text run, this skips over the entire
        ' run, not just the current character in the run.
        start = start.GetNextContextPosition(LogicalDirection.Forward)
    Loop
    Return buffer.ToString()

End Function ' End GetTextBetweenPointers.

Remarks

This method returns only uninterrupted runs of text. Nothing is returned if any symbol type other than Text is adjacent to the current TextPointer in the specified direction. Similarly, text is returned only up to the next non-text symbol.

See also

Applies to

GetTextInRun(LogicalDirection, Char[], Int32, Int32)

Copies the specified maximum number of characters from any adjacent text in the specified direction into a caller-supplied character array.

public:
 int GetTextInRun(System::Windows::Documents::LogicalDirection direction, cli::array <char> ^ textBuffer, int startIndex, int count);
public int GetTextInRun (System.Windows.Documents.LogicalDirection direction, char[] textBuffer, int startIndex, int count);
member this.GetTextInRun : System.Windows.Documents.LogicalDirection * char[] * int * int -> int
Public Function GetTextInRun (direction As LogicalDirection, textBuffer As Char(), startIndex As Integer, count As Integer) As Integer

Parameters

direction
LogicalDirection

One of the LogicalDirection values that specifies the logical direction in which to find and copy any adjacent text.

textBuffer
Char[]

A buffer into which any text is copied.

startIndex
Int32

An index into textBuffer at which to begin writing copied text.

count
Int32

The maximum number of characters to copy.

Returns

The number of characters actually copied into textBuffer.

Exceptions

startIndex is less than 0 or greater than the Length property of textBuffer.

-or-

count is less than 0 or greater than the remaining space in textBuffer (textBuffer.Length minus startIndex).

Remarks

This method returns only uninterrupted runs of text. Nothing is returned if any symbol type other than Text is adjacent to the current TextPointer in the specified direction. Similarly, text is returned only up to the next non-text symbol.

See also

Applies to