TextBox.SelectedText Property

Gets or sets the content of the current selection in the text box.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

'Declaration
Public Property SelectedText As String
'Usage
Dim instance As TextBox 
Dim value As String 

value = instance.SelectedText

instance.SelectedText = value
<object SelectedText="string" .../>

Property Value

Type: System.String
The currently selected text in the text box.

Getting this property returns a string copy of the currently selected text. Setting this property replaces the current selection with the specified string.

This example shows one way to use the SelectedText property to retrieve text that the user has selected in a TextBox control.

The following Extensible Application Markup Language (XAML) example shows the definition of a TextBox control that contains some text to select, and a Button control with a specified OnClick method.

In this example, a button with an associated Click event handler is used to retrieve the text selection. When the user clicks the button, the OnClick method copies any selected text in the textbox into a string. The particular circumstances by which the text selection is retrieved (clicking a button), as well as the action taken with that selection (copying the text selection to a string), can easily be modified to accommodate a wide variety of scenarios.

<TextBox Name="tbSelectSomeText">
  Some text to select...
</TextBox>

<Button Click="OnClick">Retrieve Selection</Button>

The following C# example shows an OnClick event handler for the button defined in the XAML for this example.

Private Sub OnClick(ByVal senter As Object, ByVal e As RoutedEventArgs)
    Dim sSelectedText As String = tbSelectSomeText.SelectedText
End Sub

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

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

.NET Framework

Supported in: 3.5, 3.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft