I have a method that I call when a the KeyUp event fires on my control and within that method I check the key pressed is a letter or digit using the char.IsLetterOrDigit method. However it doesn’t seem to work with the KeyPad numbers!?
My code is below:
void MyControls_KeyUp(object sender, KeyEventArgs e)
{
if ((char.IsLetterOrDigit(Convert.ToChar(e.KeyValue))))
{
....
}
}
Anybody any ideas why this doesn’t work?
Thanks in advance.
I’m using C#4.0
You’re not doing what you think you’re doing.
Convert.ToChar()isn’t going to take a keyboard code and turn it into the character that that key represents; there’s too much involved in doing this, what with different keyboard layouts, cultures, key modifier states, and so on. What you’re doing here is taking the numeric value of the KeyCode enumeration and casting it tochar.If you want to retrieve the character associated with a key press, you need to override the
KeyPressevent rather than theKeyUpevent. Windows Forms will do all of the necessary translation and conversion in order to determine which character corresponds to the key.