Stagger increments in C# NumericUpDown control

岁酱吖の 提交于 2019-12-22 18:48:49

问题


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.


回答1:


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



回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/3061190/stagger-increments-in-c-sharp-numericupdown-control

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!