【问题标题】:How can I accept the backspace key in the keypress event?如何在按键事件中接受退格键?
【发布时间】:2010-11-14 13:41:08
【问题描述】:

这是我的代码:

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

它允许我输入字母、数字和空格,但不允许我执行退格。请帮帮我。

【问题讨论】:

    标签: c# keypress


    【解决方案1】:

    我喜欢使用!Char.IsControl(e.KeyChar) 这样所有的“控制”字符(如退格键和剪贴板键盘快捷键)都会被免除。

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

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

    【讨论】:

    • 刚刚测试过 - 与 (char)8 的比较确实有效。
    • 使用'\b' 比使用(char)8 可能更好。
    • @AlexHumphrey 我认为更好的是(char)Keys.Back
    【解决方案2】:

    我经常使用以下两个部分:

    这个用于将文本框限制为仅整数,但允许控制键:

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

    这个用于将文本框限制为双精度,允许一个 '.'仅,并允许控制键:

    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】:

      你必须在你的句子中添加 !(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;
                  }
              }
      

      【讨论】:

        【解决方案4】:

        退格键不会由 KeyPress 事件引发。因此,您需要在 KeyDown 或 KeyUp 事件中捕获它并将 SuppressKeyPress 属性设置为 true 以防止退格键更改文本框中的文本:

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

        【讨论】:

        • 退格确实会触发按键事件
        【解决方案5】:

        对于您的问题,请在按下退格键时尝试它的工作

        e.KeyChar == ((char)Keys.Back)
        

        【讨论】:

          【解决方案6】:

          来自文档:

          KeyPress 事件不是由非字符键引发的;但是,非字符键确实会引发 KeyDown 和 KeyUp 事件。

          【讨论】:

          • 在这种情况下,文档是错误的。我已经从 KeyPress 事件中测试了 (char)8 并且它肯定被提升了。
          • +1 我对 Ed S 非常满意。为什么在我们有 key upkey down 事件时使用 type casting。在正常情况下,我们应该避免type casting。当我们没有其他选择时,我们应该使用type casting
          • 同意 Antonio 在之前的帖子和上面的 Matt Hamilton,尽管有文档,但 KeyPress 事件在退格键上引发。
          【解决方案7】:
          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;
               }
            }
          

          【讨论】:

            【解决方案8】:

            这可能会对您有所帮助

            if(Keys.KeyCode==Keys.Back){
               e.Handled =true;
             }
            

            【讨论】:

              【解决方案9】:
              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));
                  }
              }
              

              【讨论】:

              • 虽然此代码示例可能会回答这个问题,但最好在您的回答中包含一些基本解释。就目前而言,这个答案对未来的读者几乎没有任何价值。
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-08-17
              • 2016-02-13
              • 1970-01-01
              • 2012-06-04
              • 1970-01-01
              • 2011-01-22
              相关资源
              最近更新 更多