views:

1354

answers:

3

Hi,

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

+4  A: 

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

Hemant
A: 

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

davsan
+1  A: 

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

arbiter
I chose this answer because it gave me a general approach to handle this kind of an issue. Other good answers are equally good and can be used depending on the case.Thanks