Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to forbid backspace key in WPF

How can I forbid the Backspace-Key as easy as possible in a WPF-Application?

The KeyDown-Event don't catch at the DEL and the Backspace-Key.

Thank you!

like image 951
Ueli Avatar asked Oct 15 '10 10:10

Ueli


1 Answers

To handle Backspace or other pressed key in order to cancel it, try to use the "PreviewKeyDown" event handler.

In your Xaml, set the attribute PreviewKeyDown like this :

<TextBox PreviewKeyDown="textBox1_PreviewKeyDown" ...

and in your code, define the event handler like this :

private void textBox1_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Back || e.Key == Key.Delete)
    {
        e.Handled = true;
    }
}

Hop that helps :)

like image 98
GuidEmpty Avatar answered Nov 12 '22 12:11

GuidEmpty