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.

TextBox.SelectedText Property

Gets or sets a string the current selection in the TextBox.

Namespace: System.Windows.Controls
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

public string SelectedText { get; set; }
/** @property */
public String get_SelectedText ()

/** @property */
public void set_SelectedText (String value)

public function get SelectedText () : String

public function set SelectedText (value : String)

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

Property Value

A string containing or specifying the current selection in the TextBox. Getting this property returns a string copy of the current. Setting this property replaces the current selection with the specified string. This property has no default value.

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.

void OnClick(object sender, RoutedEventArgs e)
{
    String sSelectedText = tbSelectSomeText.SelectedText;
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

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

Community Additions

Show:
© 2014 Microsoft