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 - Clipboard

The fifty-ninth part of the Windows Presentation Foundation Fundamentals tutorial continues to examine the TextBoxBase class, which is a base class for text input controls. This article considers the clipboard features.


Over the last few articles in the WPF tutorial we have seen a number of members of the TextBoxBase class, which is inherited by the TextBox and RichTextBox controls. In this article we will continue the examination of this complex class by looking at the clipboard features.

To demonstrate the clipboard functionality we need a sample solution. Create a new WPF application project in Visual Studio named, "TextBoxBaseClipboardDemo". Once the project is prepared, replace the XAML in the main window with the code shown below. This creates a window containing a TextBox and three buttons.

<Window x:Class="TextBoxBaseClipboardDemo.MainWindow"
        Title="TextBoxBase Demo"
        <TextBox Name="MyTextBox" Grid.ColumnSpan="3" Margin="4" />

        <Button Grid.Row="1" Margin="4">Cut</Button>
        <Button Grid.Row="1" Grid.Column="1" Margin="4">Copy</Button>
        <Button Grid.Row="1" Grid.Column="2" Margin="4">Paste</Button>

The resultant window appears as shown below:

TextBoxBase Clipboard demo window

Cut, Copy and Paste

TextBoxes include standard clipboard functionality that does not require you to write any code. For example, users can select text in the control and use Ctrl-X, Ctrl-C and Ctrl-V to cut, copy and paste text respectively. In some situations you might want to programmatically control the clipboard features of a TextBox. You can do so using three methods: Cut, Copy and Paste.

  • Cut. Calling the Cut method against a control moves the selected text onto the clipboard and removes it from the text box. If no text is selected, the operation has no effect and any information already held on the clipboard is retained.
  • Copy. Calling the Copy method copies the selected text into the clipboard. The copied text remains in the control. If no text is selected, the clipboard is unaffected.
  • Paste. Copies the text that is already on the clipboard into the text box. If any text in the control is selected, it is replaced by the pasted information. If no selection is present, the information is pasted at the current cursor position. If the clipboard is empty, or if the contents of the clipboard are incompatible with the target control, the call has no effect.

To demonstrate, modify the XAML for the three buttons as shown below. This registers the Click event for each button.

<Button Grid.Row="1" Margin="4" Click="Cut_Click">Cut</Button>
<Button Grid.Row="1" Grid.Column="1" Margin="4" Click="Copy_Click">Copy</Button>
<Button Grid.Row="1" Grid.Column="2" Margin="4" Click="Paste_Click">Paste</Button>

You can now add the code for the buttons behind the window. Switch to the C# and add the following methods:

private void Cut_Click(object sender, RoutedEventArgs e)

private void Copy_Click(object sender, RoutedEventArgs e)

private void Paste_Click(object sender, RoutedEventArgs e)

You can now run the program and test the clipboard functionality by adding and selecting text, and clicking the buttons.

28 April 2014