I've only been doing this a few days and I'm pretty confused.
Everything else works fine, and the box only displays integers, but it still calculates with decimal values.
You can try with
UInt32 result = 0;
UInt32.TryParse("...",
new CultureInfo(""),
out result);
if(result == 0)
{
Console.WriteLine("No parsing");
}
else
{
Console.WriteLine("result={0}", result);
}
A more elegant solution would be:
If you have access to DevExpress controls, you should use a SpinEdit control. To limit its range from 0 to 999 (only integer number) set its:
Properties.Mask.EditMask
to "##0;"Properties.MaxValue
to 999Then the following line should work well without exceptions:
int myInt = Convert.ToInt32(numBox.Value);
You should be able to do a
int myInt = Convert.ToInt32(numBox.Value);
numBox.Value is a decimal, but that code will cause it to get converted to an integer.
Just know that IF you do get a decimal value back, it will round it up or down.
EDIT: Aghilas Yakoub's solution might be better if you want only positive values. My solution will convert the decimal to an int, but it could still allow for negatives. Really what you should do is set your
numBox.Minimum
to 0 so it can't go below 0.EDIT 2: If you want to warn when the value is negative, try this:
Do you want to warn if the value isn't a whole number (has decimal values)?