Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

TextPointer.GetTextInRun Method (LogicalDirection)


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

Namespace:   System.Windows.Documents
Assembly:  PresentationFramework (in PresentationFramework.dll)

public string GetTextInRun(
	LogicalDirection direction


Type: System.Windows.Documents.LogicalDirection

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

Return Value

Type: System.String

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

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.

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)

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

.NET Framework
Available since 3.0
Return to top
© 2016 Microsoft