Exception thrown by NumericUpDown control
Hi I have a NumericUpDown control which simply checks a value and if it is lower than expected then a Message box is shown.
All is well until the user holds the down key for a few seconds then just after the message box is displayed (and the user clicks OK) the following exception occurs:
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Windows.Forms.UpDownBase.UpDownButtons.TimerHandler(Object source, EventArgs args)
at System.Windows.Forms.Timer.OnTick(EventArgs e)
at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)Does anyone know why this happening and is there a work around apart from 'dont hold the button down'
Thanks for any help anyone can give me on this.
Harry
Answers
- This is a known problem and documented in this Product Feedback article. Won't fix, as usual. It is definitely a bug but a common problem when you start a modal message loop in an event handler. The control simply doesn't expect to get messages while it is executing an event handler. The workaround is to delay your response and let the control finish its event handling. Here's an example on how to do this:
private void numericUpDown1_ValueChanged(object sender, EventArgs e) {
if (numericUpDown1.Value <= 1) this.BeginInvoke(new MethodInvoker(ShowError));
}
private void ShowError() {
MessageBox.Show("test");
}
Avoid this kind of misery by just setting the Minimum property.
All Replies
- This is a known problem and documented in this Product Feedback article. Won't fix, as usual. It is definitely a bug but a common problem when you start a modal message loop in an event handler. The control simply doesn't expect to get messages while it is executing an event handler. The workaround is to delay your response and let the control finish its event handling. Here's an example on how to do this:
private void numericUpDown1_ValueChanged(object sender, EventArgs e) {
if (numericUpDown1.Value <= 1) this.BeginInvoke(new MethodInvoker(ShowError));
}
private void ShowError() {
MessageBox.Show("test");
}
Avoid this kind of misery by just setting the Minimum property. Thanks
That answered my question.


