我要如何转换"键"枚举值到" INT"字符在C#? [英] How do I convert a "Keys" enum value to an "int" character in C#?

查看:171
本文介绍了我要如何转换"键"枚举值到" INT"字符在C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎喜欢的事,应该很容易,但我有一个艰难的时间搞清楚什么需要就发生在这里。

This seems like something that should be easy, but I am having a tough time figuring out what needs to happen here.

在的KeyDown事件处理程序,如果在e.KeyValue是一个数字,我希望把它当作一个数并存储为一个int。所以,如果我打8数字键盘上的,我不希望NUMPAD8我希望int值8,我可以增加或减少或什么的。

In the "KeyDown" eventhandler, if the "e.KeyValue" is a number, I want to treat it as a number and store it as an int. So, if I hit "8" on the number pad, I don't want "Numpad8" I want the int value 8 that I can add or subtract or whatever.

所以,我怎么从键值转换为int?

So, how do I convert from the KeyValue to an int?

推荐答案

我想这个解决方案去:

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.

这篇关于我要如何转换&QUOT;键&QUOT;枚举值到&QUOT; INT&QUOT;字符在C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆