#632 – Block Input Using PreviewTextInput

You can use the PreviewTextInput event for a control that accepts text input to block certain characters from being entered into the control.

To prevent a particular character from being entered into the control, simply set the Handled property of the TextCompositionEventArgs parameter to true.  This will intercept the event routing and will prevent the control from receiving the text.

<TextBox Text="" HorizontalAlignment="Center" Width="150"
     PreviewTextInput="TextBox_PreviewTextInput" />
    private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
            // No e's allowed
            if ((e.Text == "e") || (e.Text == "E"))
                e.Handled = true;