BlackWaspTM

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.

Selecting Text

As mentioned, you can change the selection programmatically using CaretIndex, SelectionStart and SelectionLength. In addition, there are a couple of methods that can appear more readable in your code. The first of these is Select. It has two parameters, to which you supply the zero-based index of the first character to be selected and the length of the desired selection.

Let's demonstrate this using the "Select" button. Modify its XAML as shown below:

<Button Content="Select" Width="50" Margin="3" Click="SelectButton_Click"/>

We'll code the button so that when you click it, the selection is set to start after the first character in the TextBox and is five characters in length:

private void SelectButton_Click(object sender, RoutedEventArgs e)
{
    MyTextBox.Select(1, 5);
}

Try running the program to try out the new code. You may not be able to see the highlighted text when you click the "Select" button. However, if you click the "Show" button you will see that the selection has indeed changed. Note that, if you enter fewer than six characters into the TextBox, the Select method will select all of the text following the initial position. If you have fewer characters to select than required by the first argument, no text will be selected.

Selecting All TextBox Text

Sometimes you'll want to select all of the text in a TextBox. Rather than using the Select method, which would require you to determine the length of the text first, you can call the SelectAll method. No arguments are required.

To demonstrate, replace the XAML for the "All" button with the following:

<Button Content="All" Width="50" Margin="3" Click="AllButton_Click"/>

Add the following code behind the window to select all of the text when the "All" button is clicked:

private void SelectButton_Click(object sender, RoutedEventArgs e)
{
    MyTextBox.SelectAll();
}

Detecting a Change of Selection

If you wish to react to the user changing the selected text or moving the caret, you can subscribe to the TextBox's SelectionChanged event. This is raised whenever the selection is adjusted using the mouse, keyboard or other input device.

To demonstrate, register the event for the TextBox by changing the XAML, as shown below:

<TextBox Grid.Row="1"
         Name="MyTextBox"
         Margin="5"
         SelectionChanged="MyTextBox_SelectionChanged" />

Add the method below to the code behind the window. This performs the same action as the "Show" button; it displays the selected text and position properties when the selection is adjusted.

private void MyTextBox_SelectionChanged(object sender, RoutedEventArgs e)
{
    string info = string.Format(
        "'{0}' / Caret {1} / Start {2} / Length {3}",
        MyTextBox.SelectedText,
        MyTextBox.CaretIndex,
        MyTextBox.SelectionStart,
        MyTextBox.SelectionLength);

    MyLabel.Content = info;
}

Run the program, enter some text and try selecting parts of it to see the results.

Automatic Word Selection

The way in which text is selected can vary between applications. A lot of modern software uses automatic word selection. With this approach, you can use the mouse to select individual characters within a word. However, if the selection extends over multiple words, the full words are highlighted automatically. The alternative approach is where characters are selected individually, even if they span words. The second approach is almost always employed when selecting text using the keyboard.

With the TextBox control, automatic word selection is enabled by default. However, you can turn it off by setting the AutoWordSelection property's value to false.

We can demonstrate this feature in our sample code by linking the property to the CheckBox. Start by modifying the CheckBox's XAML, as follows:

<CheckBox Content="Auto"
          Margin="3 10"
          IsChecked="True"
          Checked="Auto_Checked"
          Unchecked="Auto_Checked" />

Next, add a method that sets the AutoWordSelection property to true when the CheckBox is ticked and false otherwise:

private void Auto_Checked(object sender, RoutedEventArgs e)
{
    MyTextBox.AutoWordSelection = ((CheckBox)sender).IsChecked.Value;
}

Try running the program and entering several words into the TextBox. Using the mouse and keyboard, select the text. You should find that words are selected automatically when using the mouse with the CheckBox checked.

14 February 2014