TextSelection.Select Method

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

Updates the current selection, taking two TextPointer positions to indicate the updated selection.

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

Syntax

'Declaration
Public Sub Select ( _
    anchorPosition As TextPointer, _
    movingPosition As TextPointer _
)
public void Select(
    TextPointer anchorPosition,
    TextPointer movingPosition
)

Parameters

Exceptions

Exception Condition
ArgumentException

Position specifies a position from a different RichTextBox associated with the current position.

Remarks

A TextSelection is formed from a selection between two positions indicated by two TextPointer objects. One of these positions (indicated by anchorPosition) is fixed with respect to the selection, while the other position (indicated by movingPosition) is movable. This is similar to how a user makes a selection using the mouse or keyboard.

The actual end of the new selection may be adjusted to match any selection heuristics that are applicable to the document that contains the new current selection.

Examples

The following code uses the Select method to select the last word in a RichTextBox and underline it. In this example, a space character is used as the word boundary. This code example is part of a larger example used in the TextPointer class.

Run this sample

    'This method underlines the last word in a RichTextBox
    Public Sub UnderlineLastWord()
        Dim EndofContent As TextPointer = MyRTB1.ContentEnd.GetNextInsertionPosition(LogicalDirection.Backward)
        Dim currentPointer As TextPointer = EndofContent.GetNextInsertionPosition(LogicalDirection.Backward)
        If (currentPointer Is Nothing) Then
            Return
        End If
        Dim currentChar As String = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Backward)

        While ((currentChar <> " ") _
                    AndAlso (currentChar <> ""))
            currentPointer = currentPointer.GetNextInsertionPosition(LogicalDirection.Backward)
            currentChar = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Backward)

        End While
        If (currentChar = " ") Then
            MyRTB1.Selection.Select(currentPointer.GetNextInsertionPosition(LogicalDirection.Forward), EndofContent)
        Else
            MyRTB1.Selection.Select(currentPointer, EndofContent)
        End If
        MyRTB1.Selection.ApplyPropertyValue(Run.TextDecorationsProperty, TextDecorations.Underline)
    End Sub

    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

//This method underlines the last word in a RichTextBox
public void UnderlineLastWord()
{
    TextPointer EndofContent = MyRTB1.ContentEnd.GetNextInsertionPosition(LogicalDirection.Backward);
    TextPointer currentPointer = EndofContent.GetNextInsertionPosition(LogicalDirection.Backward);

    if (currentPointer == null)
        return;

    string currentChar = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Backward);

    while (currentChar != " " && currentChar != "")
    {
        currentPointer = currentPointer.GetNextInsertionPosition(LogicalDirection.Backward);
        currentChar = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Backward);
    }

    if (currentChar == " ")
        MyRTB1.Selection.Select(currentPointer.GetNextInsertionPosition(LogicalDirection.Forward), EndofContent);
    else
        MyRTB1.Selection.Select(currentPointer, EndofContent);

    MyRTB1.Selection.ApplyPropertyValue(Run.TextDecorationsProperty, TextDecorations.Underline);
}

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.