#632 – Block Input Using PreviewTextInput
August 24, 2012 2 Comments
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; }
Pingback: Dew Drop – August 24, 2012 (#1,388) | Alvin Ashcraft's Morning Dew
Pingback: #945 – A Strategy for Limiting Allowed Text in a TextBox | 2,000 Things You Should Know About WPF