开发者

Stagger increments in C# NumericUpDown control

开发者 https://www.devze.com 2023-01-04 00:11 出处:网络
I am facing some trouble trying to configure a particular configuration for a NumericUpDown control in c#.

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 pr开发者_StackOverflow社区oblem 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.


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;            
}


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;
    }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消