#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.

Markup:
        <TextBox Name="txtSomeText"
                 CommandManager.PreviewCanExecute="txtSomeText_PreviewCanExecute"
                 Width="220" Height="25" Margin="10"/>

Code-behind:

        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.

1094-001

#945 – A Strategy for Limiting Allowed Text in a TextBox

You can limit the text that a user enters into a TextBox by handling the PreviewTextInput event and setting the TextCompositionEventArgs.Handled property to true for characters that you do not want to allow as input.

The PreviewTextInput event will not give you access to every possible keystroke that you might want to use in limiting input.  It’s not fired, for example, when the user presses the spacebar.

You often will also want to handle the PreviewKeyDown event to block keystrokes that don’t trigger PreviewTextInput.

Finally, you may want to intercept Paste events on a TextBox, in order to filter out text that you don’t want a user to paste into the TextBox.

A full strategy for limiting user-entered text might then include:

  • Handling PreviewTextInput and blocking undesirable text
  • Handling PreviewKeyDown and blocking undesirable keystrokes
  • Handling paste operations and blocking undesirable text

#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

933-001

 

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)

933-002

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.

933-003

 

#718 – Copying Text To and From the Clipboard

You can copy data to or from the Windows clipboard using static methods in the System.Windows.Clipboard class.

Here’s an example that copies text from a TextBox onto the clipboard and pastes text from the clipboard into a Label.

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <TextBox Name="txtFrom" Text="Twas the Night Before Christmas.." Margin="10"/>
        <Button Grid.Column="1" Content="Copy"
                HorizontalAlignment="Left" Padding="10,5" Margin="10"
                Click="btnCopy_Click"/>

        <Label Name="lblTo" Grid.Row="1" Content="Paste something here"
               Margin="10"/>
        <Button Grid.Row="1" Grid.Column="1" Content="Paste"
                VerticalAlignment="Top" HorizontalAlignment="Left"
                Padding="10,5" Margin="10"
                Click="btnPaste_Click"/>
    </Grid>

The Click handlers use the SetText and GetText methods of the Clipboard class to interact with the clipboard.  The SetText method copies data to the clipboard.  The GetText method pastes data from the clipboard.

        private void btnCopy_Click(object sender, RoutedEventArgs e)
        {
            Clipboard.SetText(txtFrom.Text);
        }

        private void btnPaste_Click(object sender, RoutedEventArgs e)
        {
            lblTo.Content = Clipboard.GetText();
        }

718-001

718-002

718-003

718-004

718-005