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

江枫思渺然 提交于 2019-12-18 03:49:14

问题


This is my code:

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.


回答1:


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



回答2:


I use the two following segments alot:

This one for restricting a textbox to integer only, but allowing control keys:

if (Char.IsDigit(e.KeyChar)) return;
if (Char.IsControl(e.KeyChar)) return;
e.Handled = true;

This one for restricing a textbox to doubles, allowing one '.' only, and allowing control keys:

if (Char.IsDigit(e.KeyChar)) return;
if (Char.IsControl(e.KeyChar)) return;
if ((e.KeyChar == '.') && ((sender as TextBox).Text.Contains('.') == false)) return;
if ((e.KeyChar == '.') && ((sender as TextBox).SelectionLength == (sender as TextBox).TextLength)) return;
e.Handled = true;



回答3:


You have to add !(char.IsControl(e.KeyChar)) in you sentence and that's it.

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



回答4:


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;
    }
}



回答5:


From the documentation:

The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events.




回答6:


private void KeyPressNameSurname(object sender, KeyPressEventArgs e)
 {
     if (char.IsPunctuation(e.KeyChar) || char.IsSymbol(e.KeyChar) || char.IsDigit(e.KeyChar) )
     {
        e.Handled = true;
        myTextBox.Text = "Not Valid";
        myTextBox.Visible = true;
     }
     else
     {
        myTextBox.Visible = false;
     }
  }



回答7:


private void Keypressusername(object sender, KeyPressEventArgs e)
{
    e.Handled = !(char.IsLetter(e.KeyChar));
    if (char.IsControl(e.KeyChar))
    {
        e.Handled = !(char.IsControl(e.KeyChar));
    }
    if (char.IsWhiteSpace(e.KeyChar))
    {
        e.Handled = !(char.IsWhiteSpace(e.KeyChar));
    }
}


来源:https://stackoverflow.com/questions/1191698/how-can-i-accept-the-backspace-key-in-the-keypress-event

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!