views:

57

answers:

3

Hi cloud of wisdom :-)

I am facing some trouble trying to configure a particular configuration for a NumericUpDown control in c#.

Basically, I have seen that I can set the Increment interval, but I would like to manage the Increment interval depending on which arrow is clicked (up or down). I have checked the events of the control, but I do not find an event for each arrow.

Basically, I want to achieve a control where, at different values, the increment is different.

from 0.00 to 5.00 increments of 0.01, from 5.00 to 20.00 increments of 0.04, and so on

Is this possible ?

Note: It would be useful also an historic value in the control for the last value when the valuechanged event is trigger. Does this exist?

Thanks in advance for any comment or suggestion!!!

EDIT: I edit this, because I did not explained it correctly, I guess. Here it is the reason because I would like to know which arrow was pressed, up or down.

This what I do have, more or less. I have added all the ranges, and some checkings using Modulo division to avoid incorrect values set directly in the value field using the keyboard instead of the arrows. The problem is, if I use arrow up to pass through a limit, everything is OK. However, if I use the arrow down, I miss one step.

    if (cuotaUno.Value >= 30M && cuotaUno.Value < 50M)
    {
        cuotaUno.Increment = 2M;
        if (!((cuotaUno.Value % 2M) == 0))
        {
            cuotaUno.Value = cuotaUno.Value - (cuotaUno.Value % 2M);
        }

    }

    if (cuotaUno.Value >= 50M && cuotaUno.Value < 100M)
    {
        cuotaUno.Increment = 5M;
        if (!((cuotaUno.Value % 5M) == 0))
        {
            cuotaUno.Value = cuotaUno.Value - (cuotaUno.Value % 5M);
        }

    }

In this case, If the value is 100 and I click down, it goes directly to 90 instead of 95. But, If I am at 90 and I click up, it goes to 95 and 100 correctly.

A: 

Make your own control that inerits the NumericUpDown control.

Override the OnkeyDown method, check the current value and change the increment, then call the base class's implmentation.

That's what i would try.

Bob Fincheimer
Hi Bob,first of all, thanks for your answer. Unfortunately, I do not see how the OnKeyDown method covers what I would like to do, since the control can be changed in several ways, so it does not solve the problem, as far as I understand.Thanks!!
mihe
A: 

You can do the miracle using the NumericUpDown.ValueChanged event and NumericUpDown.Increment property.

As a side note, just check the NumericUpDown.Accelerations property if it would help you as I don't know if you're very particular about the increment or the acceleration.

UPDATE

readonly decimal PART1 = 30M;
readonly decimal PART2 = 50M;
readonly decimal PART3 = 100M;
readonly decimal INC1 = 1M;
readonly decimal INC2 = 2M;
readonly decimal INC5 = 5M;
readonly decimal INC10 = 10M;

private void cuotaUno_ValueChanged(object sender, EventArgs e)
{
    decimal val = cuotaUno.Value;
    decimal inc = cuotaUno.Increment;
    decimal rem;

    if (val < PART1 && inc != INC1)
    {
        if (inc == INC2 && val == (PART1 -INC2))
            val += (inc - INC1);
        inc = INC1;
    }
    else if (val >= PART1 && val < PART2 && inc != INC2)
    {
        if (inc == INC5 && val == (PART2-INC5))
            val += (inc - INC2);
        inc = INC2;
        rem = val % INC2;
        if (rem != 0)
            val -= rem;
    }
    else if (val >= PART2 && val < PART3 && inc != INC5)
    {
        if (inc == INC10 && val == (PART3-INC10))
            val += (inc - INC5);
        inc = INC5;
        rem = val % INC5;
        if (rem != 0)
            val -= rem;
    }
    else if (val >= PART3 && inc != INC10)
    {
        inc = INC10;
        rem = val % INC10;
        if (rem != 0)
            val -= rem;
    }

    cuotaUno.Increment = inc;
    cuotaUno.Value = val;            
}
Veer
How can I know which arrow was pressed using that event and property? I do not see it :-(
mihe
could do it yourself - on each ValueChanged, store the new value somewhere, like a decimal? instance var of your subclass, then you'll be able to tell in the event both the current and last value, which sounds like is enough (if i'm parsing things right)
James Manning
@mihe: Check my Update.
Veer
Thank you Veer, I think that assigning directly the result of the calculation to the value was triggering the event again, which was opening a can of worms :-)thank you again for your answer, it turned out extremely helpful.Regards!
mihe
@mihe: `I think that assigning directly the result of the calculation to the value was triggering the event again, which was opening a can of worms` No! That was not the actual reason in your case. The reason is explained in my first comment to your answer.
Veer
+1  A: 

Wouldn't this work?

private void NumericUpDown1_ValueChanged(Object sender, EventArgs e) 
{
    if(NumericUpDown1.Value >= 0 && NumericUpDown1.Value < 5)
    {
        NumericUpDown1.Increment = 0.01;
    }
    if(NumericUpDown1.Value >= 5 && NumericUpDown1.Value < 20)
    {
        NumericUpDown1.Increment = 0.04;
    }
}
Tester101