Can I determine if a KeyEventArg is an letter or number?

一世执手 提交于 2019-12-10 01:21:17

问题


Is there a way to determine if a key is letter/number (A-Z,0-9) in the KeyEventArgs? Or do I have to make it myself? I found a way with e.KeyCode, is that accurate?

if(((e.KeyCode >= Keys.A       && e.KeyCode <= Keys.Z )
 || (e.KeyCode >= Keys.D0      && e.KeyCode <= Keys.D9 )
 || (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9))

回答1:


You can use the char.IsLetterOrDigit() method on the KeyCode of the event args:

bool isLetterOrDigit = char.IsLetterOrDigit((char) keyEventArgs.KeyCode);



回答2:


Char.IsNumber() and Char.IsLetter()




回答3:


In WPF? Use PreviewTextInput or TextInput events instead of KeyDown



来源:https://stackoverflow.com/questions/5293287/can-i-determine-if-a-keyeventarg-is-an-letter-or-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!