How do I make a textbox that only accepts numbers?

Posted by Mykroft on Stack Overflow See other posts from Stack Overflow or by Mykroft
Published on 2009-01-20T21:55:01Z Indexed on 2010/06/01 5:23 UTC
Read the original article Hit count: 403

Filed under:
|
|
|

I have a windows forms app with a textbox control that I want to only accept integer values. In the past I've done this kind of validation by overloading the KeyPress event and just removing characters which didn't fit the specification. I've looked at the MaskedTextBox control but I'd like a more general solution that could work with perhaps a regular expression, or depend on the values of other controls.

Ideally this would behave such that pressing a non numeric character would either produce no result or immediately provide the user with feedback about the invalid character.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET