Search code examples
c#.netnumericupdowneventargs

C# NumericUpDown.OnValueChanged, how it was changed?


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

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)
{

}

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.

I guess I should make my own EventArgs like this

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

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


Solution

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