Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. auto checking of NumericUpDown control

auto checking of NumericUpDown control

Scheduled Pinned Locked Moved C#
questionhelp
5 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    azusakt
    wrote on last edited by
    #1

    Hi, I just have a simple question about auto checking of input value on NumericUpDown control. Normally, if we press enter key after entered value on UpDown Control, it will automatically check whether it is between its max & min Value, and it will also limit the value. But if we just enter a value, didn't press enter key, it won't auto check the value. I know we can use Validating to solve this problem, but inside Validating method, we also will need to compare the value by ourselves, I just feel quite inconvenient if we have 10 or more updown control in a Form... Is there any build-in method of updown control that we can use to check the value ?? Thanks

    H H 2 Replies Last reply
    0
    • A azusakt

      Hi, I just have a simple question about auto checking of input value on NumericUpDown control. Normally, if we press enter key after entered value on UpDown Control, it will automatically check whether it is between its max & min Value, and it will also limit the value. But if we just enter a value, didn't press enter key, it won't auto check the value. I know we can use Validating to solve this problem, but inside Validating method, we also will need to compare the value by ourselves, I just feel quite inconvenient if we have 10 or more updown control in a Form... Is there any build-in method of updown control that we can use to check the value ?? Thanks

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Actually, both the Enter key behavior and validation work the same way - the control has to lose the focus (for validation) or the change has to be committed (by hitting the Enter key, which calls the set accessor of the Text property). See the documentation for the UpDownBase.UserEdit property in the .NET Framework SDK for a discussion of this behavior. A good way to solve this would be to extend the NumericUpDown control and override either IsInputChar or IsInputKey, which are called with each key-down. Validate the entire number, though - not just the char or Keys that was passed to the method. Then change your code to instantiate this new control instead of the NumericUpDown. It's still a NumericUpDown control, though - you only extended it. One other way is handle each NumericUpDown.KeyDown event (inherited from Control) with the same handler, in which case you could do something like this:

      numericUpDown1.KeyDown += new KeyEventHandler(ValidateNumber);
      numericUpDown2.KeyDown += new KeyEventHandler(ValidateNumber);
      //...
      private void ValidateNumber(object sender, KeyEventArgs e)
      {
      NumericUpDown nud = sender as NumericUpDown;
      if (nud != null)
      {
      // Should take the other formatting properties into account,
      // but this is only an example.
      int value = int.Parse(nud.Text);
      if (value < nud.Minimum || value > nud.Maximum)
      e.Handled = true;
      }
      }

      As the comment said, you should also take things into account like the NumericUpDown.ThousandsSeparator and the like. You can use Int32.Parse (or Int64.Parse) using the overload that uses the NumberFormatStyles as the second parameter. Do a bit-wise OR to construct the enum that you'll pass using the properties of the NumericUpDown control.

      Microsoft MVP, Visual C# My Articles

      A 1 Reply Last reply
      0
      • A azusakt

        Hi, I just have a simple question about auto checking of input value on NumericUpDown control. Normally, if we press enter key after entered value on UpDown Control, it will automatically check whether it is between its max & min Value, and it will also limit the value. But if we just enter a value, didn't press enter key, it won't auto check the value. I know we can use Validating to solve this problem, but inside Validating method, we also will need to compare the value by ourselves, I just feel quite inconvenient if we have 10 or more updown control in a Form... Is there any build-in method of updown control that we can use to check the value ?? Thanks

        H Offline
        H Offline
        Huseyin Altindag
        wrote on last edited by
        #3

        Hi I sent a solution for this problem to Ronboy in the message boards. Have a look there. hopefully it helps you. bye HuseyinAltindag

        1 Reply Last reply
        0
        • H Heath Stewart

          Actually, both the Enter key behavior and validation work the same way - the control has to lose the focus (for validation) or the change has to be committed (by hitting the Enter key, which calls the set accessor of the Text property). See the documentation for the UpDownBase.UserEdit property in the .NET Framework SDK for a discussion of this behavior. A good way to solve this would be to extend the NumericUpDown control and override either IsInputChar or IsInputKey, which are called with each key-down. Validate the entire number, though - not just the char or Keys that was passed to the method. Then change your code to instantiate this new control instead of the NumericUpDown. It's still a NumericUpDown control, though - you only extended it. One other way is handle each NumericUpDown.KeyDown event (inherited from Control) with the same handler, in which case you could do something like this:

          numericUpDown1.KeyDown += new KeyEventHandler(ValidateNumber);
          numericUpDown2.KeyDown += new KeyEventHandler(ValidateNumber);
          //...
          private void ValidateNumber(object sender, KeyEventArgs e)
          {
          NumericUpDown nud = sender as NumericUpDown;
          if (nud != null)
          {
          // Should take the other formatting properties into account,
          // but this is only an example.
          int value = int.Parse(nud.Text);
          if (value < nud.Minimum || value > nud.Maximum)
          e.Handled = true;
          }
          }

          As the comment said, you should also take things into account like the NumericUpDown.ThousandsSeparator and the like. You can use Int32.Parse (or Int64.Parse) using the overload that uses the NumberFormatStyles as the second parameter. Do a bit-wise OR to construct the enum that you'll pass using the properties of the NumericUpDown control.

          Microsoft MVP, Visual C# My Articles

          A Offline
          A Offline
          azusakt
          wrote on last edited by
          #4

          hm.... Thank you so much for your reply, Heath. I just got an idea from your reply; instead of extenting the control, I would prefer using same handler for my UpdownControls. As I have 10 updowncontrols, so I assigned the same Validating handler for each of them.

          private void UpDownValidating(object sender, System.ComponentModel.CancelEventArgs e)
          {
          NumericUpDown nud = sender as NumericUpDown;

          if(nud != null)
          {			
          if(nud.Value < nud.Minimum)
             nud.Value = nud.Minimum;
          
              if(nud.Value > nud.Maximum)
             nud.Value = nud.Maximum;
          }
          

          }

          but I don't know will it cause any hidden effect? I think this method is better and simpler for me.. One more question, Do you know any article that teaching us to make own control library. The idea is I want to make my own controls in my home, so that sometimes they can be used in my project by just import the control library. Thanks a lot~~

          H 1 Reply Last reply
          0
          • A azusakt

            hm.... Thank you so much for your reply, Heath. I just got an idea from your reply; instead of extenting the control, I would prefer using same handler for my UpdownControls. As I have 10 updowncontrols, so I assigned the same Validating handler for each of them.

            private void UpDownValidating(object sender, System.ComponentModel.CancelEventArgs e)
            {
            NumericUpDown nud = sender as NumericUpDown;

            if(nud != null)
            {			
            if(nud.Value < nud.Minimum)
               nud.Value = nud.Minimum;
            
                if(nud.Value > nud.Maximum)
               nud.Value = nud.Maximum;
            }
            

            }

            but I don't know will it cause any hidden effect? I think this method is better and simpler for me.. One more question, Do you know any article that teaching us to make own control library. The idea is I want to make my own controls in my home, so that sometimes they can be used in my project by just import the control library. Thanks a lot~~

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            Why would there be any "hidden side effect"? This is a perfectly reasonable solution, although I'd prefer encapsulation because it makes for a better OO design and doesn't force you to handle your events each time (which one could forget). Developing a control library is trivial. In VS.NET, start a new Windows Control Library. Really, all this is is a DLL project, it just starts you off with a single control (with a bad name - so don't use it). Develop your controls and components deriving from the appropriate control and component classes. Don't forget to add decent designer support. There's a good, extensive section in MSDN that describes various base classes, attributes, and other design-time features that you should take into account for a good component library. See Developing Components[^].

            Microsoft MVP, Visual C# My Articles

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups