How do I convert a "Keys" enum value to an "int" character in C#?
Something like this should work well: (Edited)
int keyVal = (int)e.KeyValue;
int value = -1;
if ((keyVal >= (int)Keys.D0 && keyVal <= (int)Keys.D9)
{
value = (int)e.KeyValue - (int)Keys.D0;
}
else if (keyVal >= (int)Keys.NumPad0 && keyVal <= (int)Keys.NumPad9)
{
value = (int)e.KeyValue - (int)Keys.NumPad0;
}
I'd go with this solution:
int value = -1;
if (e.KeyValue >= ((int) Keys.NumPad0) && e.KeyValue <= ((int) Keys.NumPad9)) { // numpad
value = e.KeyValue - ((int) Keys.NumPad0);
} else if (e.KeyValue >= ((int) Keys.D0) && e.KeyValue <= ((int) Keys.D9)) { // regular numbers
value = e.KeyValue - ((int) Keys.D0);
}
...if the point was to get the numeric value of the label of they key that was punched in.