C# WPF Key 是数字或数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14834260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Key is digit or number
提问by user13657
I have previewKeyDown
method in my window, and I'd like to know that pressed key is only A-Z
letter or 1-0
number (without anyF1..12, enter, ctrl, alt etc - just letter or number).
previewKeyDown
我的窗口中有方法,我想知道按下的键只是A-Z
字母或1-0
数字(没有任何 F1..12、输入、ctrl、alt 等 - 只是字母或数字)。
I've tried Char.IsLetter
, but i need to give the char, so e.key.ToString()[0]
doesn't work, because it is almost everytime a letter.
我试过Char.IsLetter
,但我需要给出字符,所以e.key.ToString()[0]
不起作用,因为它几乎每次都是一封信。
回答by Jon
Something like this will do:
像这样的事情会做:
if ((e.Key >= Key.A && e.Key <= Key.Z) || (e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9))
Of course you will also have to check that no modifier keys like CTRL are pressed according to your requirements.
当然,您还必须根据您的要求检查是否没有按下 CTRL 等修饰键。
回答by Igoy
Can you put some code to show what you intend? Shouldn't this work for you
你能放一些代码来显示你的意图吗?这不应该为你工作
if(e.key.ToString().Length==1)
`Char.IsLetter(e.key.ToString()[0])`
else
//
回答by Khan
e.Key
is giving you a member of the enum
System.Windows.Input.Key
e.Key
正在给你一个成员 enum
System.Windows.Input.Key
You should be able to do the following to determine whether it is a letter or a number:
您应该能够执行以下操作来确定它是字母还是数字:
var isNumber = e.Key >= Key.D0 && e.Key <= Key.D9;
var isLetter = e.Key >= Key.A && e.Key <= Key.Z;
回答by Mark Green
Add a reference to Microsoft.VisualBasic and use the VB IsNumeric function, combined with char.IsLetter().
添加对 Microsoft.VisualBasic 的引用,并结合使用 VB IsNumeric 函数和 char.IsLetter()。
回答by Rachel
In your specific case the answer provided by Jonand Jefferyis probably best, however if you need to test your string for some other letter/number logic then you can use the KeyConverterclass to convert a System.Windows.Input.Key
to a string
在您的特定情况下,Jon和Jeffery提供的答案可能是最好的,但是如果您需要测试字符串的其他字母/数字逻辑,那么您可以使用KeyConverter类将 a 转换System.Windows.Input.Key
为字符串
var strKey = new KeyConverter().ConvertToString(e.Key);
You'll still need to check to see if any modifier keys are being held down (Shift, Ctrl, and Alt), and it should also be noted that this only works for Letters and Numbers. Special characters (such as commas, quotes, etc) will get displayed the same as e.Key.ToString()
您仍然需要检查是否有任何修饰键被按下(Shift、Ctrl 和 Alt),还应该注意这仅适用于字母和数字。特殊字符(如逗号、引号等)的显示方式与e.Key.ToString()
回答by Matrix
try this, it works.
试试这个,它有效。
private void txbNumber_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key >= Key.D0 && e.Key <= Key.D9) ; // it`s number
else if (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) ; // it`s number
else if (e.Key == Key.Escape || e.Key == Key.Tab || e.Key == Key.CapsLock || e.Key == Key.LeftShift || e.Key == Key.LeftCtrl ||
e.Key == Key.LWin || e.Key == Key.LeftAlt || e.Key == Key.RightAlt || e.Key == Key.RightCtrl || e.Key == Key.RightShift ||
e.Key == Key.Left || e.Key == Key.Up || e.Key == Key.Down || e.Key == Key.Right || e.Key == Key.Return || e.Key == Key.Delete ||
e.Key == Key.System) ; // it`s a system key (add other key here if you want to allow)
else
e.Handled = true; // the key will sappressed
}