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

后端 未结 8 1914
天命终不由人
天命终不由人 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:39

    If you use the KeyPress event, the event signature has a KeyPressEventArgs with a KeyChar member that gives you the character for the numberpad keys. You can do a TryParse on that to figure out if its a number or not.

    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        int i;
        if (int.TryParse(e.KeyChar.ToString(), out i))
        {
            MessageBox.Show("Number");
        }
    }
    

提交回复
热议问题