代码之家  ›  专栏  ›  技术社区  ›  Kharlos Dominguez

按键事件-如何轻松知道按键是否为数字?

  •  9
  • Kharlos Dominguez  · 技术社区  · 15 年前

    我目前正在处理DataGridView控件的KeyDown事件。 其中一列由计算值填充,我希望用户能够根据需要重写单元格值。

    当用户按下数字键时,单元格进入编辑模式,允许用户覆盖该值。如果键不是数字,则不会发生任何事情…

    工作得很好…问题是我发现它的代码很难看… 我似乎找不到一个简单的方法来在一个条件下处理所有的数字键,所以我做了一个switch case构造来处理所有可能的数字键,如下所示:

                    switch (e.KeyCode)
                    {
                        case Keys.D0:
                        case Keys.D1:
                        case Keys.D2:
                        case Keys.D3:
                        case Keys.D4:
                        case Keys.D5:
                        case Keys.D6:
                        case Keys.D7:
                        case Keys.D8:
                        case Keys.D9:
                        case Keys.Decimal:
                        case Keys.NumPad0:
                        case Keys.NumPad1:
                        case Keys.NumPad2:
                        case Keys.NumPad3:
                        case Keys.NumPad4:
                        case Keys.NumPad5:
                        case Keys.NumPad6:
                        case Keys.NumPad7:
                        case Keys.NumPad8:
                        case Keys.NumPad9:
    
                             [code to make the cell go to editMode, etc...]
    

    当然,这是可行的,但必须有更好和更短的方法,对吗?

    我能用谷歌找到的就是把e.keycode转换成char,但当使用数字键时,它甚至会给出数字值的字母…

    谢谢。

    8 回复  |  直到 8 年前
        1
  •  11
  •   Tim Coker    15 年前

    如果您使用 KeyPress 事件,事件签名具有 KeyPressEventArgs 用一个 KeyChar 为数字键盘键提供字符的成员。你可以在上面做一个排版,看看它是不是一个数字。

    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        int i;
        if (int.TryParse(e.KeyChar.ToString(), out i))
        {
            MessageBox.Show("Number");
        }
    }
    
        2
  •  14
  •   Rawling isekaijin    15 年前

    尝试

    if ((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) ||
        (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9) ||
        e.KeyCode == Keys.Decimal)
    {
        // Edit mode
    }
    
        3
  •  7
  •   Sorcerer86pt    14 年前

    当你可以使用时,为什么要使用密码:

    void Control_KeyPress(object sender, KeyPressEventArgs e)
        {
    
            if (Char.IsDigit(e.KeyChar))
            {
                //do something
            }
            else
            {
                //do something else
            }
        }
    

    它更干净,即使微软决定改变所有的枚举vlue,它仍然可以工作。

        4
  •  6
  •   Kian    12 年前

    然而,当用户按下控制键(如退格键)时,SORCER86PT的解决方案是最简单的,然后它就会中断。要解决该问题,可以使用以下代码段:

    void KeyPress(object sender, KeyPressEventArgs e)
    {    
        if(!Char.IsNumber(e.KeyChar) && !Char.IsControl(e.KeyChar))
        {
            //The char is not a number or a control key
            //Handle the event so the key press is accepted
            e.Handled = true;
            //Get out of there - make it safe to add stuff after the if statement
            return;
        }
        //e.Handled remains false so the keypress is not accepted
    }
    

    如果您使用的是wpf,可能会发现文本框没有keypressed事件。为了解决这个问题,我使用了以下代码。

    void ValidateKeyPress(object sender, KeyEventArgs e)
    {
        char keyPressed = WPFUtils.Interop.Keyboard.GetCharFromKey(e.Key);
        if (!Char.IsNumber(keyPressed) && !Char.IsControl(keyPressed))
        {
            //As above
            e.Handled = true;
            return;
        }
    }
    

    你可能会注意到奇怪的函数调用 WPFUtils.Interop.Keyboard.GetCharFromKey(e.Key) 这是我收集的有用函数之一。 你可以找到它 here .

        5
  •  2
  •   Amir Pourmand hhafez    8 年前

    只需从键中获取最后一个字符,如果按了某个数字,该键将成为数字。 此方法用于不需要任何其他条件的键控事件。

    只需调用此静态方法并传入密钥进行检查

    public static bool IsNumber(Keys key)
    {
      string num = key.ToString().Substring(key.ToString().Length - 1);
      Int64 i64;
      if (Int64.TryParse(num, out i64))
      {
        return true;               
      }
      return false;
    }
    
        6
  •  1
  •   tanascius    15 年前

    msdn help page 他们在示例中使用此代码:

    // Determine whether the keystroke is a number from the top of the keyboard.
    if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
    

    // Determine whether the keystroke is a number from the keypad.
    if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
    
        7
  •  1
  •   bolt19    11 年前

    更精简的版本:

        private void KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !Char.IsDigit(e.KeyChar); // only allow a user to enter numbers
        }
    
        8
  •  0
  •   Gilad Green Fábio    9 年前
    void dataGridView1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        // Used this to find they key values.
        //label1.Text += e.KeyValue;
    
        // Check if key is numeric value.
        if((e.KeyValue >= 48 && e.KeyValue <= 57) || (e.KeyValue >= 97 && e.KeyValue <= 105))
            System.Console.WriteLine("Pressed key is numeric");
    }