Problem with NumericUpDown (VB.NET 2005 B2)

Hi again from Spain.

I'm working with a NumericUpDown control and i have this problem.

I set a Minimun (1) and a Maximum (200) to the control.

If the user write 210 in the control... automatically change the value to the maximum (200).

But... i don't want it. I need to show a MSGBOX with an alert and then... allow the user to change the value by hand.

How  can i deactivate this function

A very big thanks from Spain! ;)



Answer this question

Problem with NumericUpDown (VB.NET 2005 B2)

  • igkmahesh

    Sorry.  The code I gave you worked in 2003 but they've obviously beefed up the validation in 2005, as they should really.  If you want the user to change the number anyway, why does it matter if it gets reset to the maximum or minimum   At least that shows the user what is an acceptable value.  If it's still a problem, just set the NUDs max and min to the absolute max and min possible and then when you validate just compare the Value to some constants you declare yourself.

  • Yashman

    Sorry... but... don't works here... (Visual Basic 2005 Beta2).
    The maximum of NumericUpDown is 200 and Minimum = 1.


    System.ArgumentOutOfRangeException was unhandled
      Message="'500' is not a valid value for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'.
    Parameter name: Value"
      ParamName="Value"
      Source="System.Windows.Forms"
      StackTrace:
           at System.Windows.Forms.NumericUpDown.set_Value(Decimal value)

           at Almacen2005.frmCambiaCantidad.NumericUpDown1_Validating(Object sender, CancelEventArgs e) in C:\Documents and Settings\Administrador\Mis documentos\Visual Studio 2005\Projects\Almacen2005\Almacen2005\frmCambiaCantidad.vb:line 46

           at System.Windows.Forms.Control.OnValidating(CancelEventArgs e)

           at System.Windows.Forms.Control.NotifyValidating()

           at System.Windows.Forms.ContainerControl.ValidateThroughAncestor(Control ancestorControl)

           at System.Windows.Forms.ContainerControl.EnterValidation(Control enterControl)

           at System.Windows.Forms.ContainerControl.UpdateFocusedControl()

           at System.Windows.Forms.ContainerControl.AssignActiveControlInternal(Control value)

           at System.Windows.Forms.ContainerControl.ActivateControlInternal(Control control, Boolean originator)

           at System.Windows.Forms.ContainerControl.SetActiveControlInternal(Control value)

           at System.Windows.Forms.ContainerControl.SetActiveControl(Control ctl)

           at System.Windows.Forms.ContainerControl.set_ActiveControl(Control value)

           at System.Windows.Forms.Control.Select(Boolean directed, Boolean forward)

           at System.Windows.Forms.Control.SelectNextControl(Control ctl, Boolean forward, Boolean tabStopOnly, Boolean nested, Boolean wrap)

           at System.Windows.Forms.Form.ProcessTabKey(Boolean forward)

           at System.Windows.Forms.ContainerControl.ProcessDialogKey(Keys keyData)

           at System.Windows.Forms.Form.ProcessDialogKey(Keys keyData)

           at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)

           at System.Windows.Forms.ContainerControl.ProcessDialogKey(Keys keyData)

           at System.Windows.Forms.TextBoxBase.ProcessDialogKey(Keys keyData)

           at System.Windows.Forms.Control.PreProcessMessage(Message& msg)

           at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)

           at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(MSG& msg)

           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)

           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)

           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)

           at System.Windows.Forms.Application.RunDialog(Form form)

           at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)

           at System.Windows.Forms.Form.ShowDialog()

           at Almacen2005.frmSalidas.btCambiarCantidad_Click(Object sender, EventArgs e) in C:\Documents and Settings\Administrador\Mis documentos\Visual Studio 2005\Projects\Almacen2005\Almacen2005\frmSalidas.vb:line 424

           at System.Windows.Forms.Control.OnClick(EventArgs e)

           at System.Windows.Forms.Button.OnClick(EventArgs e)

           at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)

           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)

           at System.Windows.Forms.Control.WndProc(Message& m)

           at System.Windows.Forms.ButtonBase.WndProc(Message& m)

           at System.Windows.Forms.Button.WndProc(Message& m)

           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)

           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)

           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)

           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)

           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)

           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)

           at System.Windows.Forms.Application.Run(ApplicationContext context)

           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()

           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()

           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)

           at Almacen2005.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 87

           at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)

           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)

           at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)

           at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)

           at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()

           at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)

           at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)

           at Microsoft.VisualStudio.HostingProcess.Utilities.HostProc.RunUsersAssemblyDebugInZone()

           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)

           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

           at System.Threading.ThreadHelper.ThreadStart()

     

  • Luke Hoban

    The function works well, when I am changing the value in the UI. But the question is- is it possible to display the value that is greater then Max when you are loading NumericUpDown control and the value is set in the code and not in the UI The problem is - whatever I do, if the value is not between min and max, I get an exception. In the documentation it is stated: When the Value property is set, the new value is validated to be between the Minimum and Maximum values. What is the function that validates it and how do I disable it
  • Curtis Zeiszler

    Thanks for the code but don't work for me. The NumericUpDown control change the text to the Maximum. :(

    Can i disable this auto correct function

    Thanks!



  • boonpin

    I'm sure there are some syntax errors, I didn't have VS open. So I had to just type it in the Forum.



    Private Sub NumericUpDown1_Validating(ByVal sender as Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles NumericUpDown1.Validating

    Dim s as NumericUpDown
    Dim v as Double
    s = DirectCast(sender, NumericUpDown)
    If Double.TryParse(s.Text, Globalization.NumberStyles.Integer, Nothing, v) Then
       If v > s.Maximum Then
          If MessageBox.Show("Value is higher than the maximum value. Do you want to use it anyways ", "Value To High", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
             e.Cancel = False
             s.Value = Convert.ToDecimal(v)
          Else
             e.Cancel = True
          End If      
       ElseIf v < s.Minimum Then
          MessagBox.Show("Value cannot be less than: " & s.Minimum.ToString)
          e.Cancel = True
       Else
          e.Cancel = False
          v = Convert.ToDecimal(v)
       End If
    Else
       MessagBox.Show("Invalid Value")
    End If
    End Sub

     


  • Kah Geh

    I tried as well, it does seem to reset the value of the control before the validating event is called.

    I think the only thing you'll be able to do is increase the maximum, and manually check and warn from the validating event.  That way when it does validate, it will not have reset the control's value.

  • Leonid Fro

    Use the Validating event handler to check the Text and make sure it represents a valid Value.


    Private Sub NumericUpDown1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles NumericUpDown1.Validating
       Dim numericText As Double 'The number corresponding to the text currently in the nud.

       'Use TryParse in case the text is not a valid number.
       If Double.TryParse(Me.NumericUpDown1.Text, Globalization.NumberStyles.Integer, Nothing, numericText) Then
          Select Case numericText
             Case Is > Me.NumericUpDown1.Maximum
                MessageBox.Show("Too high!")
                e.Cancel =
    True
             Case Is < Me.NumericUpDown1.Minimum
                MessageBox.Show("Too low!")
                e.Cancel =
    True
             Case Else
                MessageBox.Show("Just right.")
                Me.NumericUpDown1.Value = Convert.ToDecimal(numericText)
                e.Cancel =
    False
          
    End Select
       Else
          MessageBox.Show("That ain't no number!")
          e.Cancel =
    True
       End If
    End Sub

     


  • Problem with NumericUpDown (VB.NET 2005 B2)