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

后端 未结 8 583
长情又很酷
长情又很酷 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条回答
  •  萌比男神i
    2020-12-16 11:18

    The backspace key does not raised by KeyPress event. So you need to catch it in KeyDown or KeyUp events and set SuppressKeyPress property is true to prevent backspace key change your text in textbox:

    private void txtAdd_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Back)
        {
            e.SuppressKeyPress = true;
        }
    }
    

提交回复
热议问题