This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Text Input Controls - TextBox - Selection

The forty-ninth part of the Windows Presentation Foundation Fundamentals tutorial continues to look at the TextBox control. This article describes the methods, properties and events that control text selection.


In the previous article of the WPF tutorial we looked at the basic functionality of the TextBox. This is a complex control, with many important methods, properties and events. In this article we'll expand upon the basics of plain text entry by looking at the ways in which you can work with the control's selected text. Using the members described here, you can determine which text the user has selected or change the selection programmatically.

To demonstrate the selection features we need some sample code. Create a new WPF Application in Visual Studio, naming the project, "TextBoxSelectionDemo". Once loaded, replace the XAML in the main window with that shown below:

<Window x:Class="TextBoxSelectionDemo.MainWindow"
        Title="TextBox Demo"

        <Label Content="Enter some text" Margin="5"/>
        <TextBox Grid.Row="1" Name="MyTextBox" Margin="5"/>
        <StackPanel Grid.Row="2" Orientation="Horizontal" Margin="2">
            <Button Content="Show" Width="50" Margin="3"/>
            <Button Content="Select" Width="50" Margin="3"/>
            <Button Content="All" Width="50" Margin="3"/>
            <CheckBox Content="Auto" Margin="3 10"/>
        <Label Grid.Row="3" Name="MyLabel" Margin="5"/>

The resultant window contains two Labels, a TextBox, several Buttons and a CheckBox. We'll use all of these controls in the examples.

TextBox Selection Demo Window

Obtaining the Selected Text

Possibly the most important property related to text selection is SelectedText. This returns a string containing the characters that the user has selected in a TextBox. If the user has selected all of the available text, the SelectedText and Text properties match. If no text is selected, the SelectedText property returns an empty string.

To demonstrate, register the Click event for the first button by modifying its XAML as follows:

<Button Content="Show" Width="50" Margin="3" Click="ShowButton_Click"/>

Let's display the current selection from the TextBox in the lower Label when the user clicks the button. To do so, add the following code behind the window:

private void ShowButton_Click(object sender, RoutedEventArgs e)
    MyLabel.Content = MyTextBox.SelectedText;

Run the program, enter some text into the TextBox and try clicking the "Show" button with and without text selected to see the results.

Obtaining the Selection Positions

Sometimes you won't be interested in just the text that is selected but you will want to know the position of the flashing cursor, or caret, or the highlighted region. This is important if you need to know which characters are selected in a string that is repetitive. This information is available through three properties:

  • CaretIndex. The CaretIndex property returns or sets the position of the flashing caret. The index is zero based, so if it returns zero the cursor is to the left of the first character.
  • SelectionStart. The SelectionStart property returns the same value as CaretIndex. It is also the position at which any selected text starts.
  • SelectionLength. The SelectionLength property returns the number of characters that are selected. If no selection is present, it returns zero.

To show the selected text and the three positional properties when the "Show" button is clicked, modify the Button's Click event code as follows:

private void ShowButton_Click(object sender, RoutedEventArgs e)
    string info  = string.Format(
        "'{0}' / Caret {1} / Start {2} / Length {3}",

    MyLabel.Content = info;

Run the program and try clicking the button with and without text selected to see the results.

14 February 2014