3
votes

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

2

2 Answers

5
votes

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

0
votes

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