我怎么能接受的按键事件退格键? [英] How can I accept the backspace key in the keypress event?

查看:102
本文介绍了我怎么能接受的按键事件退格键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

private void txtAdd_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!(char.IsLetter(e.KeyChar)) && !(char.IsNumber(e.KeyChar)) && !(char.IsWhiteSpace(e.KeyChar)))
    {
        e.Handled = true;
    }
}



它让我输入字母,数字和空格,但它不允许我做退格。请帮我。

It allows me to enter letters, numbers and spaces but it doesn't allow me to do backspace. Please help me.

推荐答案

我喜欢用 Char.IsControl (e.KeyChar),使所有的控制权的人物,如退格。关键和剪贴板键盘快捷键会被免除

I like to use !Char.IsControl(e.KeyChar) so that all the "control" characters like the backspace key and clipboard keyboard shortcuts are exempted.

如果你只是想检查退格键,你可以的可能的逃脱:

If you just want to check for backspace, you can probably get away with:

if (e.KeyChar == (char)8 && ...)

这篇关于我怎么能接受的按键事件退格键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆