Controlling input in Numeric Updown

Aarvee

Well-known member
Joined
Sep 21, 2005
Messages
50
Location
Bangalore, India
Programming Experience
Beginner
Hi !,

In a numericupdown control, when we set the min, max, step values in design time, all these are respected when the user uses either the updown arrow keys or selects the up/down arrows in the control. However if he enters a number, these validations go out of the limits Meaning the user can enter a value which is beyond the limits specified at design time.

One way I thought I can overcome this is to trap the keypress and make sure that the user is not able to input any data except through arrow keys. While this might work ( I will have to try it) this does not seem to be very elegant. Another alternative is to validate the input in the validating event. This again is not elegant to me since in that case there is no difference between a numeric updown control and normal text box.

Is there any other way to achieve it.

Thanks in advance.

Varadarajan R
 
When the user enters data directly into a NUD via the keyboard it is not that the Minimum and Maximum values are ignored. It is just the case that the validation is not performed until the control loses focus. There is a very good reason for this. Let's say that your NUD contains the Value 12 and you have set the Maximum to 15. If the user puts the caret between the two digits and enters a "5", then presses the Delete key, the NUD now contains 15, which is a legal Value. When the NUD loses focus it will pass validation without issue. If validation was performed as the input was entered, the user would have to delete the "2" first and then type the "5". This may not seem like a big deal but for those who naturally work the other way it would be annoying and quite possibly confusing. As for the Increment property, that was never intended to limit the possible Values. Remember that a NUD can display fractional values as well. For example, if the NUD is displaying two decimal places for currency, different people might want to set the Increment to 1 for $1, 0.1 for 10 cents or 0.01 for 1 cent. Should an increment of 1 prevent the user entering a number of cents?
 
NumericUpDown(deleting)

I have a question.
I've set up my numericupdown control with maximum/minimum and everything's working fine. However:

1. If I press delete on the control and then lose focus, the value disappears, but I've confirmed that the value is still the last value that was in there.

How can I get the control.value to either be null or 0, or the minimum value, when someone hits 'delete' and loses focus?

In the case of most numbers, the minimum value or 0 is fine, but for example, values like Body Temperature, where a nurse might not want to enter something, and doesn't want to see 0, how can I get it to null, if I've set the minimum to 90.0. So, if she deletes, I want there to be No value in the box.

Another example is that I have 2 boxes with numbers in them that I subtract and put into another control. When I delete either of these numericupdowns, the number goes away, but the total in the other control stays, because the value was still whatever it was last. In this case, I want to at least make the value 0 when a user hits delete.
 
Back
Top