1

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.

Zereges
  • 5,139
  • 1
  • 25
  • 49
  • 1
    Could you just store the previous value of the control and then compare it to the current value in the event handler? – APerson Sep 19 '14 at 16:17
  • @APerson If I had more controls of this type using same event handler, I would have to store all possible previous values during whole runtime. – Zereges Sep 19 '14 at 16:22
  • There is no built-in storage of previous content. If you need to track that to calculate value changes, you'll need to store it yourself. – Ken White Sep 19 '14 at 16:24
  • @Zereges Then make a `Dictionary` mapping `NumericUpDown`s to the values they formerly held. – APerson Sep 19 '14 at 16:24

2 Answers2

2

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));
    }
zam664
  • 757
  • 1
  • 7
  • 18
  • Well, I didn't think about using tag for this. This could do the trick, I'll probably use it. However, if I wanted to use custom event args, how would I do that? – Zereges Sep 19 '14 at 16:26
  • 1
    Just small warning for anyone trying to do the same. `(int) o.Tag` throws `InvalidCastException`, so use rather `int.Parse(o.Tag.ToString())` or `Convert.ToInt32(o.Tag)` – Zereges Sep 19 '14 at 16:43
0

You would have to create your own numeric up down control that extends the .net version, define a delegate type for your event and then hide the base controls event property.

Delegate:

public delegate void MyOnValueChangedEvent(object sender, ValueChangedEventArgs args);

Event Args Class:

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

New NumericUpDown class, hide the inherited event with 'new':

public class MyNumericUpDown : NumericUpDown
{
    public new event MyOnValueChangedEvent OnValueChanged;
}

Look here for instructions on how to raise your custom event, and here for additional information on event handling.

Jeffrey Wieder
  • 2,336
  • 1
  • 14
  • 12