3

I have code attached to the ValueChanged event of two NumericUpDown controls:

private void numericUpDownHeight_ValueChanged(object sender, EventArgs e)
{
    if (checkBoxRetainRatio.Checked)
    {
        numericUpDownWidth.Value = numericUpDownHeight.Value;
    }
}

private void numericUpDownWidth_ValueChanged(object sender, EventArgs e)
{
    if (checkBoxRetainRatio.Checked)
    {
        numericUpDownHeight.Value = numericUpDownWidth.Value;
    }
}

This works dandy when I use the controls' up/down arrows to change the value in the edit box; but if I edit the value manually (e.g., when I want to change it from 100 to 25, and can do that in six keystrokes manually whereas, incrementing by 5, it would take 15 using the down arrow), the event does not fire.

Is there a quick way to fix this rather minor irritation (IOW, if it takes something really arcane and tricky to accomplish it, I won't bother).

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

2 Answers2

5

For the ValueChanged event to occur, the Value property can be changed in code, by clicking the up or down button, or by the user entering a new value that is read by the control. The new value is read when the user hits the ENTER key or navigates away from the control. If the user enters a new value and then clicks the up or down button, the ValueChanged event will occur twice.

Source: https://msdn.microsoft.com/en-us/library/system.windows.forms.numericupdown.valuechanged%28v=vs.110%29.aspx

user700390
  • 2,287
  • 1
  • 19
  • 26
  • 2
    Basically, if you want to get the effective value while the using is still typing, you really want to register for the `TextChanged` event and parse the value yourself. – Anthony Mar 19 '15 at 14:48
  • 1
    @Anthony I would even go so far as to suggest that, if going that route, when the TextChanged event fires you should set / reset a timer, and then parse the value when the timer elapses. This way if the user is entering multiple characters, you won't parse until they finish typing. – user700390 Mar 19 '15 at 14:53
  • Totally agree, definitely a 'How deep down the rabbit hole do you want to do' situation. – Anthony Mar 19 '15 at 15:38
0

I would use sender.Value which contains the most up to date value. You do have to cast it as the type of the sender.

For the numericUpDown component:

((System.Windows.Forms.NumericUpDown)(sender)).Value.ToString()
LarsTech
  • 80,625
  • 14
  • 153
  • 225
JrBriones
  • 93
  • 1
  • 5