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.

C#
public string GetTextInRun(System.Windows.Documents.LogicalDirection direction);

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.

C#
// 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.

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

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

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.

C#
public int GetTextInRun(System.Windows.Documents.LogicalDirection direction, char[] textBuffer, int startIndex, int count);

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

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10