Question

I have several groupBoxes-controls with a NumericUpDown-control in each on of them. The NumericUpDowns have a small modification - they can also decrement in the negative range of decimal. Here is the code:

//Add KeyDown event to the numericUpDown control
numericUpDownGBPC12_angleRange.KeyDown += new KeyEventHandler(numericUpDownNegative);

The code of the function numericUpDownNegative is as follows:

void numericUpDownNegative(object sender, KeyEventArgs e)
{
    NumericUpDown temp = (NumericUpDown)sender;
    temp.Value -= temp.Increment;
    sender = (object)temp;
    NumericUpDown temp = (NumericUpDown)sender;
}

Suggestions for improving the code above are most welcome however I'm more interested if it is possible to enable negative input in a NumericUpDown. The above code works but when I try to put a negative number I get something weird. This behaviour does not apply for a non-modified NumericUpDown.

Example: Let's say numericUpDownGBPC12_angleRange has a minimum of -70.0000000000 and a maximum of 70.0000000000, which I have set by the Minimum/Maximum property parameters of the control. The starting value of the control is 0.0000000000. If I push the Down-button, I get accordingly -0.0000000001, -0.0000000002, -0.0000000003 etc. until I reach -70.0000000000. However if I decide to type -x.xxxxxxxxxx (let's say -24.2398324119) I get x-0.0000000000 (4-0.0000000000). So not only I cannot enter the full number 24 (it seems the NumericUpDown takes the last typed digit in this case, which is 4), but I get the whole part after the point completely annihilated unless it was set by using the case in which case the problem is only with the part before the point. So only the first digit (on the most left of the number) can be changed. :-/

I was thinking of using textBox-controls however the amount of number fields I have as part of the interface will create a huge overhead because of the parsing of each and every textBox (we all know that sadly many users love to experiment with things that where never intended to be experimented with ;)) to make sure a certain number is entered. Despite the negative-thingy the NumericUpDown has really nice feature such as - only a digit can be entered and you can also specify the precision, the range of values etc.

So again the question is - is it possible for a NumericUpDown to accept negative input by the user?

Was it helpful?

Solution

Problem was in the KeyDown-event (had to remove it completely) and also in the format I was trying to input as a number. I have the ',' seperator and not the '.' in my Visual Studio (due to localization). So typing '.' made the NumericUpDown go berserk.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top