开发者

How to forbid backspace key in WPF

开发者 https://www.devze.com 2023-01-19 22:54 出处:网络
How can I forbid the Backspace-Key as easy as possible in a WPF-Application? The KeyDo开发者_运维知识库wn-Event don\'t catch at the DEL and the Backspace-Key.

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

The KeyDo开发者_运维知识库wn-Event don't catch at the DEL and the Backspace-Key.

Thank you!


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 :)


Try overriding OnTextInput(...).

Then if(args.Text == "\b") should give you the backspace.

0

精彩评论

暂无评论...
验证码 换一张
取 消