Partager via


TextBox.SelectionLength Property

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

Gets or sets the number of characters in the current selection in the text box.

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

Syntax

'Declaration
Public Property SelectionLength As Integer
public int SelectionLength { get; set; }
<TextBox SelectionLength="int"/>

Property Value

Type: System.Int32
The number of characters in the current selection in the text box, or 0 if there is no selection.

Exceptions

Exception Condition
ArgumentOutOfRangeException

The value is less than 0.

Remarks

Getting this property returns the number of characters in the current selection. Setting this property adjusts the length of the current selection to the specified value, keeping the beginning of the selection fixed.

If the specified selection length is longer than the amount of text, the selection length automatically adjusts so that the resulting selection is never more than the amount of text.

A SelectionChanged event is raised when a valid SelectionLength is set.

Examples

The following code example shows how you can use the SelectionLength property.

Private Sub SelectionLength()

    'The first five characters in MyTB1 are selected. 
    MyTB1.SelectionLength = 5
End Sub
private void SelectionLength()
{

    //The first five characters in MyTB1 are selected.
    MyTB1.SelectionLength = 5;
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

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