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

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.

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"); } }.

– Rawling Jul 1 '10 at 11:12 Thanks Tim and Rawling. That's the solution I went with. I also have to handle a DEL key event in KeyDown... I would have preferred to keep it all at the same spot, but this code looks by far the nicest.

Thanks everyone else as well. – Kharlos Dominguez Jul 1 '10 at 11:31.

Try if ((e. KeyCode >= Keys. D0 && e.

KeyCode = Keys. NumPad0 && e. KeyCode NumPad9) || e.

KeyCode == Keys. Decimal) { // Edit mode }.

On the msdn help page they use this code in their example: // Determine whether the keystroke is a number from the top of the keyboard. If (e. KeyCode Keys.

D9) ... // Determine whether the keystroke is a number from the keypad. If (e. KeyCode Keys.

NumPad9).

Thanks. – Kharlos Dominguez Jul 1 '10 at 11:10 As bad as a practice as that may be, I would hope that since Microsoft is using that in their docs, they would be careful to keep the behavior consistent. If you can switch to the KeyPress event instead of KeyDown, you can use the solution I provided.

– Tim Coker Jul 1 '10 at 11:12 Yeah, if there's a reason not to do that, Tim's solution is good - and makes it a lot more obvious what you're trying to do, too. – Rawling Jul 1 '10 at 11:15 @Kharlos: I understand the issues you have, but think about what they would have to change to make your code break. The enum can change without any problem as long as they do not change the order of the NumPad?

And D? Elements. And I think that there is no reason to define NumPad9 before "NumPad8 ... – tanascius Jul 1 '10 at 11:19 Yeah, I agree with you that is very unlikely that they would do that.

It is more a rhetorical issue than a real-world one, but if I can, I might as well not try to break a best practice, even if it is unlikely it would pose any problem in the real-world. – Kharlos Dominguez Jul 1 '10 at 11:34.

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 = 97 && e. KeyValue WriteLine("Pressed key is numeric"); }.

Why use keycodes, when you can use this: void Control_KeyPress(object sender, KeyPressEventArgs e) { if (Char. IsDigit(e. KeyChar)) { //do something } else { //do something else } } It's cleaner and even if microsoft decides to change all enums vlue, it still would work.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions