在文本框中过滤键盘输入,C#

时间:2022-08-22 12:20:08

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

0表示仅数字1表示字母表仅2表示字母数字

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#?

因此,如果配置了0并且键盘上按下了字符“a”,则它不会显示在文本框中。我如何在C#中做到这一点?

Thanks

3 个解决方案

#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).

您需要订阅控件的KeyPress事件(以及可选的KeyDown方法),如果必须使用键击,请将Handled属性设置为true。在msdn中阅读更多内容(包含您的问题的示例)。

#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

你可以使用正则表达式。根据您获得的用户输入,您可以更改正则表达式/验证方法。下面是一篇文章,解释了如何扩展文本框以验证正则表达式。希望有助于至少有一些想法。 http://www.c-sharpcorner.com/UploadFile/scottlysle/RegExTextBox01022007150131PM/RegExTextBox.aspx

#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).

您需要订阅控件的KeyPress事件(以及可选的KeyDown方法),如果必须使用键击,请将Handled属性设置为true。在msdn中阅读更多内容(包含您的问题的示例)。

#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

你可以使用正则表达式。根据您获得的用户输入,您可以更改正则表达式/验证方法。下面是一篇文章,解释了如何扩展文本框以验证正则表达式。希望有助于至少有一些想法。 http://www.c-sharpcorner.com/UploadFile/scottlysle/RegExTextBox01022007150131PM/RegExTextBox.aspx