views:

408

answers:

8

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 that difficult to write your own, it's not trivial either.

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

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

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

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

+3  A: 

There is the NumericUpDown control which is made specifically for the input of numbers and can be used like a TextBox.

Garry Shutler
+4  A: 

You could use a MaskedTextBox

Cameron MacFarland
Create an instance of this control and set its Mask property as documented here 'http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.mask.aspx'
Gishu
+3  A: 

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.

sh_kamalh
+4  A: 

I second Garry Shutlers recommendation of using NumericUpDown. You might not like the up-down-controls, but that is the standard look of a numeric entry control in Windows, and you should think twice about using a different look.

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.

Rasmus Faber
A: 

You can also, derive the TextBox class and grab the keypad event and ensure nothing other than numbers is written.

If it were a Web page, the same would have been done to an html text box using Javascript.

Syed Sajid Nizami
A: 

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.

In this case, however, I think that the FilteredTextBox provides the functionality you describe.

Kramii
+1  A: 

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 ES_NUMBER style has not been implemented.

You could possibly handle the HandleCreated event (or override OnHandleCreated, as TextBox isn't sealed) and call SetWindowLong to set the ES_NUMBER style on the underlying Edit control. ES_NUMBER is defined as 0x2000 in WinUser.h.

Mike Dimmick
A: 

Based on the second edit:

The Windows Forms FAQ tells you how to restrict characters in a textbox in question 26.12:

26.12 How can I restrict the characters that my textbox can accept?

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.

See the FAQ for the code example.

Cameron MacFarland