简体   繁体   English

在C#文本框中过滤来自键盘的输入

[英]Filter input from keyboard in Textbox, C#

I want to filter the keyboard inputs into textbox based on the type of input I allow. 我想根据允许的输入类型将键盘输入过滤到文本框中。 eg 例如

0 for Digits only 1 for Alphabets only 2 for Alphanumerics 0(仅数字)1(仅字母)2(字母数字)

So if 0 is configured and a character 'a' is pressed on the keyboard, it is not shown in the textbox. 因此,如果配置了0并在键盘上按下了字符“ a”,则它不会显示在文本框中。 How do I do that in C#? 如何在C#中做到这一点?

Thanks 谢谢

不确定我是否正确理解了您的问题,但是您可以使用带遮罩的文本框来创建多种类型的输入过滤器。

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. 您需要订阅控件的KeyPress事件(以及可选的KeyDown方法),并且如果必须吃掉击键,请将Handled属性设置为true。 Read more in msdn (with sample that cover your problem). msdn中阅读更多信息(包含解决您的问题的示例)。

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 http://www.c-sharpcorner.com/UploadFile/scottlysle/RegExTextBox01022007150131PM/RegExTextBox.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM