C#NumericUpDown控件中的交错增量 [英] Stagger increments in C# NumericUpDown control

查看:334
本文介绍了C#NumericUpDown控件中的交错增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

这可能吗?

注意:触发valuechanged事件时,控件中的历史值也可以用作最后一个值.是否存在?

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!!!

我对此进行了编辑,因为我没有正确解释.这是因为我想知道向上或向下按下了哪个箭头.

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

    }

在这种情况下,如果值是100,而我单击向下,则直接转到90,而不是95.但是,如果我是90,并且我单击向上,则正确地转到95和100.

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.

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

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.

更新

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

这篇关于C#NumericUpDown控件中的交错增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆