C# NumericUpDown 控件中的交错增量

发布于 2024-09-05 21:19:43 字数 1093 浏览 11 评论 0原文

我在尝试为 C# 中的 NumericUpDown 控件配置特定配置时遇到一些麻烦。

基本上,我已经看到可以设置增量间隔,但我想根据单击的箭头(向上或向下)来管理增量间隔。我已经检查了控件的事件,但没有找到每个箭头的事件。

基本上,我想实现一个控制,在不同的值下,增量是不同的。

从 0.00 到 5.00 增量为 0.01,从 5.00 到 20.00 增量为 0.04,依此类推

这可能吗?

注意:当触发 valuechanged 事件时,控件中最后一个值的历史值也很有用。这存在吗?

预先感谢您的任何意见或建议!

编辑:我编辑这个,因为我想我没有正确解释它。这就是原因,因为我想知道按下了哪个箭头,向上还是向下。

这或多或少是我所拥有的。我添加了所有范围,并使用模除法进行了一些检查,以避免使用键盘而不是箭头直接在值字段中设置不正确的值。问题是,如果我使用向上箭头穿过限制,则一切正常。然而,如果我使用向下箭头,我就会错过一步。

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

    }

在本例中,如果值为 100 并且我单击向下,它会直接变为 90 而不是 95。但是,如果我在 90 处并且单击向上,它会正确变为 95 和 100。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

隐诗 2024-09-12 21:19:43

您可以使用 NumericUpDown.ValueChanged 创造奇迹 事件和 NumericUpDown.Increment< /a> 属性。

作为旁注,只需检查 NumericUpDown。 Accelerations 属性是否对您有帮助,因为我不知道您是否对增量或加速度非常挑剔。

更新

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

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;            
}
太阳公公是暖光 2024-09-12 21:19:43

这不行吗?

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

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;
    }
}
暖心男生 2024-09-12 21:19:43

创建您自己的控件来初始化 NumericUpDown 控件。

重写 OnkeyDown 方法,检查当前值并更改增量,然后调用基类的实现。

这就是我要尝试的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文