Files
docs-desktop/dotnet-desktop-guide/framework/wpf/controls/how-to-retrieve-a-text-selection.md
T
Andy De George da363692ff Initial WPF content migrated (#17)
* Reset branch for WPF changes

* Convert BMP to PNG; fix link-out-of-scope err

* Add snippets for WPF... 6794 files!!!!

* Add missing snippets

* update file updated between migration

* Fix paths to include

* update breadcrumb and toc

* fix index links

* fix index links

* fix index links

* fix markdown
2020-09-04 09:46:28 -07:00

2.2 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Retrieve a Text Selection 03/30/2017
csharp
vb
text [WPF], retrieving
TextBox control [WPF], retrieving text
retrieving text [WPF]
d5793172-1e11-4a39-9be0-73f336ed858d

How to: Retrieve a Text Selection

This example shows one way to use the xref:System.Windows.Controls.TextBox.SelectedText%2A property to retrieve text that the user has selected in a xref:System.Windows.Controls.TextBox control.

Example

The following [!INCLUDETLA#tla_xaml] example shows the definition of a xref:System.Windows.Controls.TextBox control that contains some text to select, and a xref:System.Windows.Controls.Button control with a specified xref:System.Windows.Controls.Button.OnClick%2A method.

In this example, a button with an associated xref:System.Windows.Controls.Primitives.ButtonBase.Click event handler is used to retrieve the text selection. When the user clicks the button, the xref:System.Windows.Controls.Button.OnClick%2A 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.

[!code-xamlTextBox_MiscCode#_TextBoxSelectTextXAML]

Example

The following C# example shows an xref:System.Windows.Controls.Button.OnClick%2A event handler for the button defined in the [!INCLUDETLA2#tla_xaml] for this example.

[!code-csharpTextBox_MiscCode#_SelectText] [!code-vbTextBox_MiscCode#_SelectText]

See also