<p>Does anyone know why Microsoft does not ship a numeric text box with its .NET framework e.g. a text box which would ensure that the characters entered are always a valid number? It's something which is commonly used across applications of different flavours and indeed something which most GUI libraries (well, those that I know) deliver in some way. While it's not <em>that</em> difficult to write your own, it's not trivial either.</p>

<p>So, I'm interested in finding out if anyone can rationalise this omission.</p>

<p>edit: Thanks for the suggestions. Whilst masked text boxes and numeric up-downs have their place; I am interested in a control that looks like a text box but automatically performs validation on key press that the input corresponds to a valid number. In my (admittedly limited) experience, this is something which is used quite a bit (we don't always want the static constraints imposed by masked text boxes, just as we don't always want the up-down controls at the side). </p>

<p>There are lots of implementations with varying degrees of quality of this on the net and indeed there's even an example of this on the <a href="http://msdn.microsoft.com/en-us/library/ms229644.aspx" rel="nofollow noreferrer">MSDN</a>. </p>

<p>edit2: Thanks guys, so it sounds like the numeric up-down is the .NET control to use for numeric input only (and the reason why we don't actually have an explicit numeric text box control). It would have been great if it automatically disallowed the input of non-numeric characters (on keypress, on paste etc) but I guess it's good enough that it performs the validation when the control loses focus. And, one could do the on keypress, on paste validation if one were really keen... </p>

[{'answer_id': 81117, 'author': 'Garry Shutler', 'author_id': 6369, 'author_profile': 'https://Stackoverflow.com/users/6369', 'pm_score': 2, 'selected': False, 'text': '<p>There is the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.numericupdown.aspx" rel="nofollow noreferrer">NumericUpDown</a> control which is made specifically for the input of numbers and can be used like a TextBox.</p>\n'}, {'answer_id': 81134, 'author': 'Cameron MacFarland', 'author_id': 3820, 'author_profile': 'https://Stackoverflow.com/users/3820', 'pm_score': 2, 'selected': False, 'text': '<p>You could use a <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.aspx" rel="nofollow noreferrer">MaskedTextBox</a></p>\n'}, {'answer_id': 81140, 'author': 'sh_kamalh', 'author_id': 9794, 'author_profile': 'https://Stackoverflow.com/users/9794', 'pm_score': 2, 'selected': False, 'text': '<p>Starting with WinForms 2.0, you have a MaskedTextBox. You can set the mask to whatever you want, i.e. for numbers use the mask all 0s.</p>\n'}, {'answer_id': 81340, 'author': 'Rasmus Faber', 'author_id': 5542, 'author_profile': 'https://Stackoverflow.com/users/5542', 'pm_score': 3, 'selected': True, 'text': '<p>I second Garry Shutlers recommendation of using NumericUpDown. You might not like the up-down-controls, but that <em>is</em> the standard look of a numeric entry control in Windows, and you should think twice about using a different look.</p>\n\n<p>If you end up coding your own implementation (or finding one on the web), there are some pitfalls to look out for. Remember that there are many ways for a value to get into a control besides keypresses. The one in your link on MSDN does not even override pasting, so you can easily ctrl-V a non-numeric string into the control.</p>\n'}, {'answer_id': 81421, 'author': 'sajidnizami', 'author_id': 9498, 'author_profile': 'https://Stackoverflow.com/users/9498', 'pm_score': 0, 'selected': False, 'text': '<p>You can also, derive the TextBox class and grab the keypad event and ensure nothing other than numbers is written. </p>\n\n<p>If it were a Web page, the same would have been done to an html text box using Javascript. </p>\n'}, {'answer_id': 81721, 'author': 'Kramii', 'author_id': 11514, 'author_profile': 'https://Stackoverflow.com/users/11514', 'pm_score': 0, 'selected': False, 'text': '<p>Microsoft leave it to 3rd parties to fill in the gaps regarding missing controls in the toolbox. I imagine time and cost would feature in their rationale.</p>\n\n<p>In this case, however, I think that the <a href="http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/FilteredTextBox/FilteredTextBox.aspx" rel="nofollow noreferrer">FilteredTextBox</a> provides the functionality you describe.</p>\n'}, {'answer_id': 81853, 'author': 'Mike Dimmick', 'author_id': 6970, 'author_profile': 'https://Stackoverflow.com/users/6970', 'pm_score': 1, 'selected': False, 'text': '<p>Some of the .NET Framework controls oddly do not expose all the features of the underlying Windows control that they wrap. In this case, for some reason the <code>ES_NUMBER</code> style has not been implemented.</p>\n\n<p>You could possibly handle the HandleCreated event (or override OnHandleCreated, as TextBox isn\'t sealed) and call <a href="http://www.pinvoke.net/default.aspx/user32/SetWindowLong.html" rel="nofollow noreferrer">SetWindowLong</a> to set the <code>ES_NUMBER</code> style on the underlying Edit control. <code>ES_NUMBER</code> is defined as 0x2000 in WinUser.h.</p>\n'}, {'answer_id': 89361, 'author': 'Cameron MacFarland', 'author_id': 3820, 'author_profile': 'https://Stackoverflow.com/users/3820', 'pm_score': 0, 'selected': False, 'text': '<p>Based on the second edit:</p>\n\n<p>The <a href="http://www.syncfusion.com/faq/windowsforms/default.aspx" rel="nofollow noreferrer">Windows Forms FAQ</a> tells you how to restrict characters in a textbox in question <a href="http://www.syncfusion.com/faq/windowsforms/faq_c94c.aspx#q830q" rel="nofollow noreferrer">26.12</a>:</p>\n\n<p><strong>26.12 How can I restrict the characters that my textbox can accept?</strong></p>\n\n<p><em>You can handle the textbox\'s KeyPress event and if the char passed in is not acceptable, mark the events argument as showing the character has been handled. Below is a derived TextBox that only accepts digits (and control characters such as backspace, ...). Even though the snippet uses a derived textbox, it is not necessary as you can just add the handler to its parent form.</em></p>\n\n<p>See the FAQ for the code example.</p>\n'}]
['https://Stackoverflow.com/questions/81104', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4368/']