TextSelection.Text Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets the plain text contents of the current selection.

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

Syntax

'Declaration
Public Property Text As String
public string Text { get; set; }

Property Value

Type: System.String
A string that contains the plain text contents of the current selection.

Remarks

Use this property to extract the plain text content in the current selection, regardless of any formatting that may be present.

In this property, new line characters and paragraph breaks are treated as equivalent. Any content breaks in the current selection are converted to new lines when this property is read.

Examples

The following code shows a method that takes a RichTextBox named RTB and a TextPointer named pointer. It uses the Text property to return the string in RTB that is represented by pointer.

    Private Function GetCurrentChar(ByVal RTB As RichTextBox, ByVal pointer As TextPointer, ByVal direction As LogicalDirection) As String
        Dim nextPointer As TextPointer = pointer.GetNextInsertionPosition(direction)
        If (Not (nextPointer) Is Nothing) Then
            RTB.Selection.Select(pointer, nextPointer)
            If (RTB.Selection.Text.Length <> 0) Then
                Return RTB.Selection.Text(0).ToString
            End If
        End If
        Return ""
    End Function

private string GetCurrentChar(RichTextBox RTB, TextPointer pointer, LogicalDirection direction)
{
    TextPointer nextPointer = pointer.GetNextInsertionPosition(direction);
    if (nextPointer != null)
    {
        RTB.Selection.Select(pointer, nextPointer);
        if (RTB.Selection.Text.Length != 0)
            return RTB.Selection.Text[0].ToString();
    }
    return "";
}

Version Information

Silverlight

Supported in: 5, 4

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.