NumericUpDown ValueChanged event with immediate consequences?

Ben picture Ben · Jun 23, 2012 · Viewed 13.7k times · Source

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.

Answer

GrayFox374 picture GrayFox374 · Jun 23, 2012

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