Export (0) Print
Expand All

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
)

Parameters

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


.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft