If you're looking for a specific built-in numeric type (e.g. How To Distinguish Between Philosophy And Non-Philosophy? If you want to limit the user for number of digit, use: textBox1.MaxLength = 2; // this will allow the user to enter only 2 digits How to make HTML input tag only accept numerical values? I have asked the same question before, the thread becomes messy somewhat. What is "stdafx.h" used for in Visual Studio? This solution is reinventing the wheel with caveats. what if the first character itself is not a digitwouldn't subtracting 1 in that case throw an error. Also, using TextChanged instead of KeyPress creates a bit of recursion in that the code will jump into a second TextChanged event after the Remove method. Is it realistic for an actor to act in four movies in six months? Making statements based on opinion; back them up with references or personal experience. But a simple click event that activates on rightclick would suffice wouldnt it? Here we have used the KeyPress event to limit our textbox to numeric values only. But you can Handle text after merging multiline values. 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. It takes a simple mask format so you can limit the input to numbers or dates or whatever. You could also add a check for '-' if your TextBox should allow negative values. I don't know if my step-son hates me, is scared of me, or likes me? For example, using this class, it is possible to create "RegexedTextBox" which will accept only strings which match specific regular expression: After that, inheriting from the "RegexedTextBox" class, we can easily create "PositiveNumberTextBox" and "PositiveFloatingPointNumberTextBox" controls: Sorry to wake the dead, but I thought someone might find this useful for future reference. What does "you better" mean in this context of conversation? All contents are copyright of their authors. No votes so far! HTML 5 You can use HTML5 input type number to restrict only number entries: <input type="number" name="someid" /> This will work only in HTML5 complaint browser. Can state or city police officers enforce the FCC regulations? Browse other questions tagged, Where developers & technologists share private knowledge with coworkers, Reach developers & technologists worldwide. It works by intercepting the TextChanged event. This might be useful. This is demonstrated below: HTML 1 2 NumericUpDown is actually a collection of controls containing a 'spin box' (up down buttons), a text box and some code to validate and wange-jangle it all together. June 15, 2022 by PBPhpsolutions. By clicking Accept all cookies, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy. The problem is that if I've already entered say. Allow pressing Numpad numbers. Can your method do this? //only allows numeric values in the textbox. By clicking Accept all cookies, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy. From what I can see it's only possible to see the string before the character was entered? By clicking Post Your Answer, you agree to our terms of service, privacy policy and cookie policy. I have variable for the whole window and I am using. Localization for example. I'm relatively new to Windows Forms, and it's quite a jungle of functionality & MSDN docs, so also thanks for the specific doc pointer to. (event.keyCode>=65 && event.keyCode<=90 ) && event.keyCode!=32);">,