#1,094 – Disabling Editing Operations in a TextBox

You can disable the cut/copy/paste features in a TextBox by handling the CommandManager.PreviewCanExecute event and checking for the associated editing commands.

TextBox has implicit command bindings to support cut, copy, and paste commands.  When the user executes one of these commands, using the context menu or a keyboard shortcut, the associated routed command is executed, with the TextBox as the source.  You can short-circuit the command by intercepting PreviewCanExecute and making sure that CanExecute returns false.

        <TextBox Name="txtSomeText"
                 Width="220" Height="25" Margin="10"/>


        private void txtSomeText_PreviewCanExecute(object sender, CanExecuteRoutedEventArgs e)
            if ((e.Command == ApplicationCommands.Cut) ||
                (e.Command == ApplicationCommands.Copy) ||
                (e.Command == ApplicationCommands.Paste))
                e.Handled = true;
                e.CanExecute = false;

The editing functions are now disabled. The context menu in the TextBox shows the commands greyed out and corresponding keyboard shortcuts do nothing.



#933 – Cut/Copy/Paste Functionality in a TextBox

Cut, Copy and Paste functionality is automatically enabled for the text within a TextBox control.

You can select text in a TextBox by

  • Left-clicking and dragging with the mouse
  • Double-clicking to select a word
  • Moving the arrow keys while holding down the Shift key
  • Pressing Ctrl+A to select all text in the TextBox



With text selected, you can Cut or Copy selected text by

  • Right-clicking and selecting Cut or Copy from the context menu
  • Pressing Ctrl+X to Cut text (remove from TextBox and add to clipboard)
  • Pressing Ctrl+C to Copy text (add to clipboard without removing)


You can Paste text from the clipboard into the TextBox at the current cursor location by

  • Right-clicking and selecting Paste from the context menu
  • Pressing Ctrl+V to Paste text

If you currently have text selected, doing a Paste operation will replace the selected text with text from the clipboard.