KeyDown 事件 - 如何轻松知道按下的键是否是数字?
我当前正在处理 DataGridView 控件的 KeyDown 事件。 其中一列由计算值填充,我希望用户能够根据需要覆盖单元格值。
当用户按下数字键时,单元格进入编辑模式并允许用户覆盖该值。如果密钥不是数字,则不会发生任何事情...
这工作得很好...问题是我发现它的代码很难看... 我似乎无法找到一种简洁的方法来处理单个条件下的所有数字键,因此我制作了一个 switch case 构造来处理所有可能的数字键,如下所示:
switch (e.KeyCode)
{
case Keys.D0:
case Keys.D1:
case Keys.D2:
case Keys.D3:
case Keys.D4:
case Keys.D5:
case Keys.D6:
case Keys.D7:
case Keys.D8:
case Keys.D9:
case Keys.Decimal:
case Keys.NumPad0:
case Keys.NumPad1:
case Keys.NumPad2:
case Keys.NumPad3:
case Keys.NumPad4:
case Keys.NumPad5:
case Keys.NumPad6:
case Keys.NumPad7:
case Keys.NumPad8:
case Keys.NumPad9:
[code to make the cell go to editMode, etc...]
当然,它可以工作,但是有是一个更好、更短的方法,对吗?
我使用 Google 所能找到的就是将 e.KeyCode 转换为字符,但是当使用数字键时,它甚至会给出数字值的字母......
谢谢。
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 value if they want.
When the user presses a numeric key, the cell goes into EditMode and allows the user to override the value. If the key is not numeric, nothing happens...
That is working pretty well... the problem is that I find the code for it ugly...
I can't seem to find a neat way to handle all the numeric keys in a single condition, so I've made a switch case construct to deal with all the possible numeric keys, like this:
switch (e.KeyCode)
{
case Keys.D0:
case Keys.D1:
case Keys.D2:
case Keys.D3:
case Keys.D4:
case Keys.D5:
case Keys.D6:
case Keys.D7:
case Keys.D8:
case Keys.D9:
case Keys.Decimal:
case Keys.NumPad0:
case Keys.NumPad1:
case Keys.NumPad2:
case Keys.NumPad3:
case Keys.NumPad4:
case Keys.NumPad5:
case Keys.NumPad6:
case Keys.NumPad7:
case Keys.NumPad8:
case Keys.NumPad9:
[code to make the cell go to editMode, etc...]
Sure, it works, but there has to be a better and shorter way, right?
All I could find using Google is converting e.KeyCode to a char, but when using numeric keys, it goes gives letters even for the numeric values...
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
尝试
Try
如果您使用
KeyPress
事件,则事件签名具有一个KeyPressEventArgs
和一个KeyChar
成员,该成员为您提供数字键盘按键的字符。您可以对其进行 TryParse 以确定它是否是数字。If you use the
KeyPress
event, the event signature has aKeyPressEventArgs
with aKeyChar
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.Sorcerer86pt 的解决方案是最简单的,但是,当用户按下控制键(例如退格键)时,它就会崩溃。要解决该问题,您可以使用以下代码片段:
如果您使用 WPF,您可能会发现 TextBox 没有 KeyPressed 事件。为了解决这个问题,我使用了以下代码。
您可能会注意到奇怪的函数调用
WPFUtils.Interop.Keyboard.GetCharFromKey(e.Key)
这是我收集的有用函数之一。您可以在此处找到它< /a>.
Sorcerer86pt's solution was the simplest, however, when a user presses a control key, like backspace, then it breaks. To solve that problem, you can use the following snippet:
If you're using WPF, you might find that a TextBox doesn't have a KeyPressed event. To fix this, I used the following code.
You may notice the weird function call
WPFUtils.Interop.Keyboard.GetCharFromKey(e.Key)
this is one of the useful functions I've collected.You can find it here.
为什么要使用键码,当你可以使用它时:
它更干净,即使微软决定更改所有枚举 vlue,它仍然可以工作
Why use keycodes, when you can use this:
It's cleaner and even if microsoft decides to change all enums vlue, it still would work
在 msdn 帮助页面上,他们使用此他们的示例中的代码:
...
On the msdn help page they use this code in their example:
...
更精简一点的版本:
A bit more condensed version:
只需从按键中获取最后一个字符,如果按下数字,该字符将是数字。
此方法适用于 KeyDown 事件,不需要任何其他条件。
只需调用这个静态方法并传入Key即可检查
Just get the last char from the Key that will be number if a number was pressed.
This method works with KeyDown events not needing any other conditions.
Just call this static method and pass in the Key to check