Filter input from keyboard in Textbox, C#

我怕爱的太早我们不能终老 提交于 2019-12-13 02:49:33

问题


I want to filter the keyboard inputs into textbox based on the type of input I allow. e.g.

0 for Digits only 1 for Alphabets only 2 for Alphanumerics

So if 0 is configured and a character 'a' is pressed on the keyboard, it is not shown in the textbox. How do I do that in C#?

Thanks


回答1:


You need to subscribe to control's KeyPress event (and optionally KeyDown method), and if key stroke must be eaten set Handled property to true. Read more in msdn (with sample that cover your problem).




回答2:


Not sure I understood your question correctly but you can use masked text box for creating many types of input filters.




回答3:


you can use regular expressions. according to the user input you get, you can change your regular expression/validation method. below is an article that explains how to extend a textbox to validate against regular expressions. hope that helps at least to have some idea. http://www.c-sharpcorner.com/UploadFile/scottlysle/RegExTextBox01022007150131PM/RegExTextBox.aspx



来源:https://stackoverflow.com/questions/1036870/filter-input-from-keyboard-in-textbox-c-sharp

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