Thursday, June 25, 2009

NumericUpDown validation

Today I had the problem that I had to set up input validation on an edit form. Among other controls there was also a NumericUpDown (NUD) control and I thought: well if I specifiy the min value and the max value then the user is not able to input a wrong value;
This code prints the current value of the NUD to a message box:
MessageBox.Show(this.numericUpDown1.Value.ToString());
You can see an example for this in the following screenshot:


This works fine, but there is one exception. It is also possible to leave the NUD blank. If you show then the value of the NUD again with the above code then you get the last value that was present in the NUD control; as shown below:


This can also be explained. The value property of the NUD control is of type decimal in order to deal with the different types of numbers. The problem is that it is of type decimal and not decimal? (this is the nullable version of decimal) and therefore it has to have value. Now is the question which value to set if the control is empty: 0, -1, decimal.MinValue or decimal.MaxValue?? None of these value really expresses what the user has entered; namely nothing.
In a forum in the internet I read that a workaround for this issue is to check the Text property of the NUD control; but as you can see below the NUD control does not have such a property!!


Fortunately there exists a solution to this problem. If you want to check if the user has entered a number into the NUD control you can use this code to check this:
if (string.IsNullOrEmpty(((Control)this.numericUpDown1).Text))
{
/* the user entered nothing */
}
else
{
/* the NUD contains a valid number */
}

10 comments:

Frank Gennaro said...

Absolutely brilliant! Many thanks! I had the same problem and could not come up with a solution, but thanks to you, I now have it. What timing! You posted your code yesterday and I was looking for a solution just this morning.

Unknown said...

Thanks; I'm always happy if a post on my blog helps someone... :-)

drew said...

This post was a huge help to me as well! Thanks for sharing this solution!

Nissim said...

I accidently ran into this post.
It seems weird to me that a NumericUpDown needs to be casted to Control. so here's the catch: NumericUpDown 'Text' property has attributes of:
EditorBrowsable(EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)

in other words - it's there! just because the IntelliSense doesn't show it - doesn't means it doesn't exist.
You can simply use:
NUD.Text and it will compile.

Unknown said...

Hi (don't know your name),
many thanks for the hint. I tried it and you are absolutely right. I already wondered on how Microsoft did to hide a public property of the base class. I will modify my post accordingly as soon as possible.
Thanks :-)

Abdul Wahab said...

Thank you so much..

Anonymous said...

thanks ניסים
your suggestion helped a lot.
by d wy how di u know it?

dendroid66 said...
This comment has been removed by the author.
dendroid66 said...

Yes, it works fine!
On the other hand, I use it to reset my NUD:

(((Control)this.numUpDown).Text) = string.Empty;

Thank you.

Anonymous said...

Thanks man!!!!!!