C# 如何在按键事件中接受退格键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1191698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I accept the backspace key in the keypress event?
提问by
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.
它允许我输入字母、数字和空格,但不允许我使用退格键。请帮我。
回答by Matt Hamilton
I like to use !
Char.IsControl(e.KeyChar) so that all the "control" characters like the backspace key and clipboard keyboard shortcuts are exempted.
我喜欢使用!
Char.IsControl(e.KeyChar) 以便免除所有“控制”字符,例如退格键和剪贴板键盘快捷键。
If you just want to check for backspace, you can probablyget away with:
如果您只想检查退格,您可能会逃脱:
if (e.KeyChar == (char)8 && ...)
回答by Ed S.
From the documentation:
从文档:
The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events.
KeyPress 事件不是由非字符键引发的;但是,非字符键确实会引发 KeyDown 和 KeyUp 事件。
回答by Wanabrutbeer
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;
回答by Huy Nguyen
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:
KeyPress 事件不会引发退格键。因此,您需要在 KeyDown 或 KeyUp 事件中捕获它并将 SuppressKeyPress 属性设置为 true 以防止退格键更改文本框中的文本:
private void txtAdd_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Back)
{
e.SuppressKeyPress = true;
}
}
回答by naveen kumar
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));
}
}
回答by Nasuh Levent YILDIZ
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;
}
}
回答by Gaby
You have to add !(char.IsControl(e.KeyChar)) in you sentence and that's it.
你必须在你的句子中添加 !(char.IsControl(e.KeyChar)) ,就是这样。
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;
}
}
回答by Sepehr Ariamehr
for your problem try this its work for when backspace key pressed
对于你的问题,试试这个它在按下退格键时的工作
e.KeyChar == ((char)Keys.Back)