【新手求助} numericUpDown控件问题

hejie13250 2014-07-31 03:18:11
求大神指点
numericUpDown控件问题,如下:
当value最大时点Up,value就变为最小值,
当value最小时点Down,value就变为最大值,
我本想通过鼠标的相对坐标来判断点的是哪一个按钮,但是行不通,值为22时点击后就成了0,值为0时点击后就成了22,代码如下,
        private void numericUpDown1_ValueChanged(object sender, EventArgs e) 
{
int i = MousePosition.Y - this.Location.Y;

if (numericUpDown1.Value == 23 && i < 165)
{
numericUpDown1.Value = 0;
}
if (numericUpDown1.Value == 0 && i > 165)
{
numericUpDown1.Value = 23;
}
Trace.WriteLine(numericUpDown1.Value);
}
...全文
93 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhouzangood 2014-07-31
  • 打赏
  • 举报
回复
hejie13250 2014-07-31
  • 打赏
  • 举报
回复
发错了!!!!!应该是这样!
private void numericUpDown1_ValueChanged(object sender, EventArgs e)    //定时 小时
{
    int i = MousePosition.Y - this.Location.Y;
    if (numericUpDown1.Value == 24 && i < 165)
    {
numericUpDown1.Value = 0;
    }
    if (numericUpDown1.Value == -1 && i > 165)
    {
numericUpDown1.Value = 23;
    }
    Trace.WriteLine(numericUpDown1.Value);
}
hejie13250 2014-07-31
  • 打赏
  • 举报
回复
多谢二楼和三楼两位的热心帮助,我昨天才开始学C#,表示看不懂,不过我的问题已经搞定了,喟然不完美,方法如下: 把最大值增1,最小值减1,然后修改一下就OK了.
        private void numericUpDown1_ValueChanged(object sender, EventArgs e) 
        {
            int i = MousePosition.Y - this.Location.Y;
 
            if (numericUpDown1.Value == 23 && i < 165)
            {
                numericUpDown1.Value = 0;
            }
            if (numericUpDown1.Value == 0 && i > 165)
            {
                numericUpDown1.Value = 23;
            }
            Trace.WriteLine(numericUpDown1.Value);
        }
我姓区不姓区 2014-07-31
  • 打赏
  • 举报
回复

        public Form2()
        {
            InitializeComponent();

            this.numericUpDown1.Maximum = 23;
            this.numericUpDown1.Minimum = -1;
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            if (this.numericUpDown1.Value == 23)
                this.numericUpDown1.Value = 0;
            else if (this.numericUpDown1.Value == -1)
                this.numericUpDown1.Value = 22;
        }
ArmStronger 2014-07-31
  • 打赏
  • 举报
回复
可以重写一个组件继承NumericUpdown,然后重写方法UpButton()和DownButton(),如下:
public partial class NumericUpDownEx : NumericUpDown
    {

        public NumericUpDownEx()
        {
            InitializeComponent();
            
        }

        public override void UpButton()
        {
            base.UpButton();
            if (this.Value >= Maximum)
                this.Value = Minimum;
        }

        public override void DownButton()
        {
            base.DownButton();
            if (this.Value <= Minimum)
                this.Value = Maximum;
        }
       
    }

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧