C#NumericUpDown.OnValueChanged,它是如何改变的? [英] C# NumericUpDown.OnValueChanged, how it was changed?

查看:694
本文介绍了C#NumericUpDown.OnValueChanged,它是如何改变的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下如何为现有事件处理程序创建自定义EventArgs.

I would like to ask how to make custom EventArgs for existing event handler.

让我们说,我拥有NumericUpDown numericUpDown控件,并且想要其OnValueChanged事件的处理程序.双击Visual Studio中的ValueChanged可以使代码段像这样

Lets say, that I have NumericUpDown numericUpDown control and I want handler for its OnValueChanged event. Double clicking to ValueChanged in visual studio makes snippet like this

private void numericUpDown_ValueChanged(object sender, EventArgs e)
{

}

但是,我想知道它是如何更改的(例如+ 5,-4.7),但是普通的EventArgs没有此信息.也许Decimal change = Value - Decimal.Parse(Text)可以解决问题(由于延迟的文本更改),但这是丑陋的方式,可能无法每次都起作用.

However, I'd like to know how it was changed (like +5, -4.7), but plain EventArgs does not have this information. Maybe Decimal change = Value - Decimal.Parse(Text) would do the trick (because of delayed text change), but that's ugly way and may not work every single time.

我想我应该像这样制作自己的EventArgs

I guess I should make my own EventArgs like this

class ValueChangedEventArgs : EventArgs
{
    public Decimal Change { get; set; }
}

,然后以某种方式覆盖NumericUpDown.OnValueChanged事件,以生成具有适当信息的EventArgs.

and then somehow override NumericUpDown.OnValueChanged event to generate my EventArgs with proper information.

推荐答案

仅标记最后一个值可能会容易得多.

It may be much easier to just tag the last value.

    private void numericUpDown1_ValueChanged(object sender, EventArgs e) {
        NumericUpDown o = (NumericUpDown)sender;
        int thisValue = (int) o.Value;
        int lastValue = (o.Tag == null) ? 0 : (int) o.Tag;
        o.Tag = thisValue;
        MessageBox.Show("delta = " + (thisValue - lastValue));
    }

这篇关于C#NumericUpDown.OnValueChanged,它是如何改变的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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