KeyDown event - how to easily know if the key pressed is numeric?

后端 未结 8 1887
天命终不由人
天命终不由人 2020-12-19 03:00

I am currently handling the KeyDown event of a DataGridView control. One of the columns is filled by calculated values and I want the user to be able to override the cell va

8条回答
  •  粉色の甜心
    2020-12-19 03:51

    Just get the last char from the Key that will be number if a number was pressed. This method works with KeyDown events not needing any other conditions.

    Just call this static method and pass in the Key to check

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

提交回复
热议问题