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 Base Classes - TextBoxBase - Undo and Redo

The sixtieth part of the Windows Presentation Foundation Fundamentals tutorial continues the examination of the TextBoxBase class, a base class for text input controls. This article considers the in-built undo and redo capabilities.

TextBoxBase

In several previous articles in the WPF tutorial we have examined various sets of members of the TextBoxBase class. This is a base class that is inherited by the TextBox and RichTextBox controls. In this article we will continue to look at this large class, this time reviewing the undo and redo functionality. These features allow you to programmatically undo changes to the text in the control and to revert changes that have been undone.

To demonstrate, let's create a sample solution. Create a new WPF application project in Visual Studio named, "TextBoxBaseUndoDemo". Once ready, replace the XAML in the main window with the code shown below.

<Window x:Class="TextBoxBaseUndoDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TextBoxBase Demo"
        Width="250"
        Height="200">
    <StackPanel>
            <TextBox Name="MyTextBox"
                 Height="75"
                 TextWrapping="Wrap"
                 TextChanged="MyTextBox_TextChanged" />
            <StackPanel Orientation="Horizontal">
                <Button Width="100" Margin="5" Click="Undo_Click">Undo</Button>
                <Button Width="100" Margin="5" Click="Redo_Click">Redo</Button>
            </StackPanel>
        <CheckBox Name="CanUndoCheckBox" IsEnabled="False">Can Undo</CheckBox>
        <CheckBox Name="CanRedoCheckBox" IsEnabled="False">Can Redo</CheckBox>
        <CheckBox Name="IsUndoEnabledCheckBox"
                  IsChecked="True"
                  Click="IsUndoEnabledCheckBox_Click">Is Undo Enabled</CheckBox>
    </StackPanel>
</Window>

In order to make the program build we need to add the code for the declared events. For now, switch to the code behind the window and add the following empty methods. We'll add code to these in the remaining sections of this article.

private void IsUndoEnabledCheckBox_Click(object sender, RoutedEventArgs e)
{

}

private void Undo_Click(object sender, RoutedEventArgs e)
{

}

private void Redo_Click(object sender, RoutedEventArgs e)
{

}

private void MyTextBox_TextChanged(object sender, TextChangedEventArgs e)
{

}

You can now run the program to see the window design. It appears similar to the image below:

TextBoxBase Undo/Redo demo window

Undo and Redo

WPF text boxes provide standard undo and redo facilities. If the user presses Ctrl-Z, the last change to the text is reversed. Where there are multiple changes, Ctrl-Z can be used several times to undo each change in turn. Once this feature has been used, the user can press Ctrl-Y to reverse one or more undo operations.

Sometimes you will want to control the undo or redo processes, calling them programmatically. To enable this, the TextBoxBase class defines two simple methods, Undo and Redo. As you might expect, Undo reverses the last change and Redo reverts an undo operation. Neither method has any parameters.

We can demonstrate the two methods using the event code for the Undo and Redo buttons. Modify the two methods as shown below:

private void Undo_Click(object sender, RoutedEventArgs e)
{
    MyTextBox.Undo();
}

private void Redo_Click(object sender, RoutedEventArgs e)
{
    MyTextBox.Redo();
}

Run the program and type some text into the TextBox before clicking the two buttons. To see the way that multiple changes can be undone, type some text, click Undo and then Redo, then add extra text. It will now be possible to undo twice.

9 May 2014