关于datagrid keydown事件的Isletter [英] Isletter on datagrid keydown event

查看:72
本文介绍了关于datagrid keydown事件的Isletter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过计算他们的输入是字母还是数字来将用户输入分解为DataGrid上的KeyDown事件。



方法I我现在正在使用表演很棒。首先,IsDigit从未发生过,显然无法正常工作。其次,IsLetter可以工作,但仅限于某些字母(W,Z,X,V和Y)。必须有一个更全面的方法,因为这显然不适合我。



我尝试过:


这是我到目前为止使用的方法;



I am attempting to break down user input onto a KeyDown event on a DataGrid by working out whether their input is a letter or a number.

The method I am using at the moment performs bizzarely. Firstly, the IsDigit never happens and clearly is not working. Secondly the IsLetter works, but only on some letters (W,Z,X,V and Y). There must be a more comprehensive way of doing this as this clearly is not working for me.

What I have tried:

This is the method I am using so far;

private void OnDataGridKeyDown(object sender, KeyEventArgs e)
{
   if (char.IsLetter(Convert.ToChar(e.Key)))
   {
        MessageBox.Show("Letter");
   }
   if (char.IsDigit(Convert.ToChar(e.Key)))
   {
        MessageBox.Show("Number");
   }
}

推荐答案

Convert.ToChar没有显式获取Key值的重载,所以它会产生什么我不确定。它不太可能将Key.A转换为'A',Key.B转换为'B'等等。

事实上,我刚检查过:

Convert.ToChar does not have an overload which explicitly takes a Key value, so quite what it will produce I'm not certain. It's unlikely to convert Key.A to 'A', Key.B to 'B' and so forth.
In fact, I just checked:
System.Windows.Input.Key k = System.Windows.Input.Key.A;
for (int i = 0; i < 26; i++)
    {
    char c = Convert.ToChar(k);
    Console.WriteLine("{0}:{1}", k, c);
    k++;
    }

并且它没有:

And it doesn't:

A:,
B:-
C:.
D:/
E:0
F:1
G:2
H:3
I:4
J:5
K:6
L:7
M:8
N:9
O::
P:;
Q:<
R:=
S:>
T:?
U:@
V:A
W:B
X:C
Y:D
Z:E



你需要做的是:


What you need to do is:

Key k = e.Key;
if (k >= Key.A && k <= Key.Z)
    {
    ...
    }
if ((k >= Key.D0 && k <= Key.D9) || (k >= Key.NumPad0 && k <= Key.NumPad9))
    {
    ...
    }


这篇关于关于datagrid keydown事件的Isletter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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