C
Christoph Nahr
Everyone knows by now that the NumericUpDown control does not fire the
ValueChanged event when the user has manually entered a value.
However, I just discovered that manually entered values are also never
restricted to the Minimum and Maximum values, not even when the user
tabs away from the control!
The attached sample program creates a NUD control with a value range
of 1-10, and a button whose sole purpose is to allow tabbing away from
the NUD control. Note that you can manually enter whatever value you
want, and the value will persist even after tabbing back and forth.
The only workaround that I've found is to attach a Validating event
handler that cancels any change that would cause the value to leave
the range defined by Minimum and Maximum.
Is there some secret trick that I'm missing? Thanks...
----- C# sample code follows -----
using System;
using System.Drawing;
using System.Windows.Forms;
public class WindowsForm: Form {
public static void Main() {
Application.Run(new WindowsForm());
}
public WindowsForm() {
NumericUpDown upDown = new NumericUpDown();
upDown.Location = new Point(10, 10);
upDown.Minimum = 1m;
upDown.Maximum = 10m;
Controls.Add(upDown);
Button button = new Button();
button.Location = new Point(upDown.Right + 10, 10);
button.Text = "OK";
Controls.Add(button);
}
}
ValueChanged event when the user has manually entered a value.
However, I just discovered that manually entered values are also never
restricted to the Minimum and Maximum values, not even when the user
tabs away from the control!
The attached sample program creates a NUD control with a value range
of 1-10, and a button whose sole purpose is to allow tabbing away from
the NUD control. Note that you can manually enter whatever value you
want, and the value will persist even after tabbing back and forth.
The only workaround that I've found is to attach a Validating event
handler that cancels any change that would cause the value to leave
the range defined by Minimum and Maximum.
Is there some secret trick that I'm missing? Thanks...
----- C# sample code follows -----
using System;
using System.Drawing;
using System.Windows.Forms;
public class WindowsForm: Form {
public static void Main() {
Application.Run(new WindowsForm());
}
public WindowsForm() {
NumericUpDown upDown = new NumericUpDown();
upDown.Location = new Point(10, 10);
upDown.Minimum = 1m;
upDown.Maximum = 10m;
Controls.Add(upDown);
Button button = new Button();
button.Location = new Point(upDown.Right + 10, 10);
button.Text = "OK";
Controls.Add(button);
}
}