1

我有一个带有一些 numreicUpDown 控件的 WinForm,我想知道该值是增加还是减少。控件触发两种情况下更改的事件值,据我所知,程序调用方法 UpButton 和 DownButton。有没有其他方法可以知道值是如何更改的,或者我是否必须使用这种方法来执行此操作(例如触发事件或在 Up-Down-Button 中实现我的代码)

4

2 回答 2

3

没有标准的方法可以做到这一点。我建议记住旧值并将其与新值进行比较

decimal oldValue;

private void ValueChanged(object sender, EventArgs e)
{
    if (numericUpDown.Value > oldValue)
    {
    }
    else
    {
    }
    oldValue = numericUpDown.Value;
}
于 2012-09-24T07:46:49.663 回答
1

创建您自己的控件来覆盖这些 UpButton 和 DownButton 方法:

using System.Windows.Forms;
public class EnhancedNUD : NumericUpDown
{
    public event EventHandler BeforeUpButtoning;
    public event EventHandler BeforeDownButtoning;
    public event EventHandler AfterUpButtoning;
    public event EventHandler AfterDownButtoning;

    public override void UpButton()
    {
        if (BeforeUpButtoning != null) BeforeUpButtoning.Invoke(this, new EventArgs());
        //Do what you want here...
        //Or comment out the line below and do your own thing
        base.UpButton();
        if (AfterUpButtoning != null) AfterUpButtoning.Invoke(this, new EventArgs());
    }
    public override void DownButton()
    {
        if (BeforeDownButtoning != null) BeforeDownButtoning.Invoke(this, new EventArgs());
        //Do what you want here...
        //Or comment out the line below and do your own thing
        base.DownButton();
        if (AfterDownButtoning != null) AfterDownButtoning.Invoke(this, new EventArgs());
    }
}

然后,当您在表单上实现控件时,您可以连接一些事件,让您知道单击了哪个按钮或按下了键(上/下)。

于 2016-12-16T22:02:18.497 回答