Filter input from keyboard in Textbox, C#


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



You need to subscribe to control's KeyPress event (and optionally KeyDown method), and if key stroke must be eaten set <strong>Handled</strong> property to true. Read more in <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx" rel="nofollow">msdn</a> (with sample that cover your problem).


Not sure I understood your question correctly but you can use <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.aspx" rel="nofollow">masked text box</a> for creating many types of input filters.


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. <a href="http://www.c-sharpcorner.com/UploadFile/scottlysle/RegExTextBox01022007150131PM/RegExTextBox.aspx" rel="nofollow">http://www.c-sharpcorner.com/UploadFile/scottlysle/RegExTextBox01022007150131PM/RegExTextBox.aspx</a>


  • regex to grab text if code exists
  • Send Facebook chat messages via JavaScript
  • Add completion handler to presentViewControllerAsSheet(NSViewController)?
  • Create event for arrow keys in extjs
  • Control key + click event on google map
  • Pygame the rectangle is not moving?
  • QML ListElement pass list of strings
  • Creating a Messenger service
  • time_t conversion format question
  • KeyDown not picking up 'Return' key C#
  • NServiceBus - NServiceBus.Host as publisher and WPF app as subscriber. How To?
  • Ionic2: Unsubscribe Event to Avoid Duplicate Entries?
  • How to trigger a KeyPressed event for unit testing in JAVA
  • Is it possible to disable esc/F11 key during full screen mode of webpage, programmatically?
  • Can't delete li from to-do list
  • How can I let users share their location in Bot Framework webchat channel?
  • d3 v4 drag and drop with TypeScript
  • JQuery Internet Explorer and ajaxstop
  • Database structure design with variable amounts of fields
  • Why value captured by reference in lambda is broken? [duplicate]
  • Nant, Vault & Windows Integrated Authentication
  • How do I fake an specific browser client when using Java's Net library?
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • Bug in WPF DataGrid
  • How to get a value (ex: baseURL) in every Karate feature?
  • Trying to switch camera back to front but getting exception
  • jQuery tmpl and DataLink beta
  • How can I estimate amount of memory left with calling System.gc()?
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • Why joiner is not used after Sequence generator or Update statergy
  • Linking SubReports Without LinkChild/LinkMaster
  • sending mail using smtp is too slow
  • Checking variable from a different class in C#
  • Programmatically clearing map cache
  • Busy indicator not showing up in wpf window [duplicate]
  • Recursive/Hierarchical Query Using Postgres
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS