Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello, I have a problem in the validation of NumericUpDown control in VB.net. I want it to accept only the numbers, but it is accepting punctuations like '.' and apostrophe also, here my code what I did below, but still the problem persists, I used keypress:
Posted
Updated 5-May-11 13:11pm
v2

1 solution

Not the best if/then/else structure you got there, but hey.

Anyway, Why can you not just set the DecimalPlaces property of the NumbericUpDown control to 0?

Then when you need to retrieve the data value you can use Math.Floor function to convert to the correct int value (i.e. no rounding applied (e.g. 2.6 still converts to 2))
 
Share this answer
 
Comments
Marc A. Brown 13-Apr-11 11:12am    
Good answer. And you're right about that if/else.
555336 13-Apr-11 12:07pm    
no you have not understood, i thought the numeric accepts only numbers that it is generated, but in reality when you are typing on it, it seems it is accepting . and '
musefan 13-Apr-11 12:18pm    
Yes, if you set it to decimal places = 0 then it will still accept "." but it will not display - however, and what I think is a bug, the underlying Value will be a decimal including any fractions you typed in, that is why you use Math.Floor to ensure you have the correct Int value that you want.

Basically, you do not want a message box popping up. This is not user friendly!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900