7

I'm writing a small application in C# (.NET 4.0). I want to fire an event when value changes in numericUpDown, but it seems that numericUpDown ValueChanged fires when control loses focus or when there are multiple changes.

Long description: I am binding (OnPropertyChanged) numericUpDown with a property of an object, so that the changes are reflected immediately. But I also need to calculate something (global / not related to object) using this newly changed value of numericUpDown. But if I use ValueChanged event, it fires too late.

Short description:

I need an event with similar functionality to OnPropertyChanged.

What can I do?

EDIT: In ValueChanged event handler I did some calculations on the objects, but the value that has changed wasn't yet committed to the object property, thus I thought that the ValueChanged event was lagging behind.

Thank you for your help.

Ben
  • 2,395
  • 5
  • 41
  • 56
  • The ValueChanged event is definitely equivalent to an OnPropertyChanged kind of event. If you want to reduce the amount of calculations then consider using the Leave event. – Hans Passant Jun 23 '12 at 12:05
  • 1
    @HansPassant The problem is that if I use the arrows to increase number, the ValueChanged event doesn't fire (it fires if I press the arrow again, but it fires for the previous change). – Ben Jun 23 '12 at 13:01
  • 1
    I get no repro for that at all. Post code that reproduces the problem. – Hans Passant Jun 23 '12 at 13:04
  • This happens when the numbers are entered in keyb, not using the up/down arrows. Just imagine the control shows "1" and you need to get to "5000", you'll just type 5000 because it's easier than holding the up arrow til it gets to 5000. If you type the number the event won't fire on every key press, it looks like it fires on lost focus. – Jcis Aug 07 '19 at 20:11

1 Answers1

9

ValueChanged is what you need to use. I checked and it is not firing when the control loses focus, and I can't enter multiple changes as you claim. If I click three times really fast, the message box shows the expected value from the first click, so that's what you want. I don't get where you say ValueChanged fires too late. It's showing the new value in the message box even before it's repainted on the screen.

    private void numericUpDown1_ValueChanged(object sender, EventArgs e)
    {
        MessageBox.Show("Value changed to " + numericUpDown1.Value.ToString());
    }
GrayFox374
  • 1,722
  • 9
  • 13