首页 新闻 会员 周边

C#自定义NumericUpDown控件无法实现预想效果

0
悬赏园豆:80 [已解决问题] 解决于 2013-03-14 15:58

先上代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace NumericEx
{
    /************************************************************************/
    /* 希望实现的效果
     * 1.当数据在设定的最大值和最小值之间时,颜色是正常色
       2.当数据超出最大值时,颜色变为红色
       3.当数据低于最小值时,颜色变为蓝色
       4.当数据由超出最值范围变为在最值范围内时,颜色由异常色变为正常色*/
    /************************************************************************/
    public class NumericEx : NumericUpDown
    {
        /*public NumericEx() : base()
        {

        }*/

        public void SetValue(Decimal newValue, Decimal newMax, Decimal newMin, Decimal oldMax, Decimal oldMin)
        {
            if (newMax > oldMax)
            {
                this.Maximum = newMax;
                base.Maximum = newMax;
                this.Minimum = oldMin;
                this.BackColor = Color.Red;
                base.BackColor = Color.Red;
            }
            if (newMin < oldMin)
            {
                this.Minimum = newMin;
                this.Maximum = oldMax;
                this.BackColor = Color.SkyBlue;
            }
            this.Value = newValue;
            if (newMax <= oldMax)
            {
                this.Maximum = oldMax;
                this.Minimum = oldMin;
                this.BackColor = SystemColors.Window;
            }
            if (newMin >= oldMin)
            {
                this.Minimum = oldMin;
                this.Maximum = oldMax;
                this.BackColor = SystemColors.Window;
            }
            //this.Refresh();
        }
    }
}

引用部分:

//控件设定的最值范围是0~10
private void button2_Click(object sender, EventArgs e)
        {

            numericEx1.SetValue(12M, 12M, 0M, 10M, 0M);
          
        }

 

问题是在调用如上函数后,控件新的最值范围设定不成功,导致控件的值只能是最大值。

 

想问各位博友,应该如何实现我的预期效果呢?

luoshupeng的主页 luoshupeng | 初学一级 | 园豆:68
提问于:2013-01-24 08:27
< >
分享
最佳答案
0

SetValue函数写得有问题

luoshupeng | 初学一级 |园豆:68 | 2013-03-14 15:57
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册