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

TextSelection.Select Method

Silverlight

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

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

public void Select(
	TextPointer anchorPosition,
	TextPointer movingPosition
)

Parameters

anchorPosition
Type: System.Windows.Documents.TextPointer
A fixed anchor position that marks one end of the updated selection.
movingPosition
Type: System.Windows.Documents.TextPointer
A movable position that marks the other end of the updated selection.

ExceptionCondition
ArgumentException

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

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.

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 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 "";
}


Silverlight

Supported in: 5, 4

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

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

Community Additions

Show:
© 2014 Microsoft