Windows > Windows Forms Forums > Windows Forms General > Exception thrown by NumericUpDown control
Ask a questionAsk a question
 

AnswerException thrown by NumericUpDown control

  • Thursday, May 03, 2007 3:48 PMHarryBoy123 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Thursday, May 03, 2007 4:31 PMnobugzMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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

  • Thursday, May 03, 2007 4:31 PMnobugzMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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.

  • Thursday, May 03, 2007 5:43 PMHarryBoy123 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Thanks

    That answered my question.