How can I accept the backspace key in the keypress event?

后端 未结 8 620
长情又很酷
长情又很酷 2020-12-16 10:39

This is my code:

private void txtAdd_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!(char.IsLetter(e.KeyChar)) && !(char.IsNumber(e.KeyChar)         


        
8条回答
  •  没有蜡笔的小新
    2020-12-16 11:20

    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 && ...)
    

提交回复
热议问题