Click here to Skip to main content
5,788,212 members and growing! (18,773 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » Custom Controls     Intermediate License: The Code Project Open License (CPOL)

Numeric UpDown Control for hardware control applications or yet another SpinEdit

By Igor Voynovskyy

An article on numeric Up/Down control with some unique features useful for hardware control applications
C#, .NET, WinForms, Dev

Posted: 18 Jun 2008
Updated: 18 Jun 2008
Views: 6,410
Bookmarked: 13 times
Note: This is an unedited reader contribution
Announcements
Loading...



Search    
Advanced Search
Sitemap
7 votes for this Article.
Popularity: 3.89 Rating: 4.60 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 14.3%
3
2 votes, 28.6%
4
4 votes, 57.1%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

For many of my projects, which are mainly related to controlling hardware, I need a numeric UpDown (SpinEdit) control which could easily and quickly change value extending along many orders of magnitude, let say to change frequency from 1 to 2 GHz and then add 200 Hz more. Unfortunately standard controls provided by Borland VCL or .Net do not satisfy this requirement. Search among third party controls was not successful too as I needed some specific capabilities, most notably the value late update. Under the late update I mean that for hardware control application it is common that SpinEdit controls value on remote device and thus it is desirable to initiate value change by SpinEdit, but postpone actual Value change until hardware confirms that parameter has been changed.

The simple IgSpinEdit control offered in this article provides required functionality. It was originally developed for Borland C Builder, but as I started to use .Net some time ago I ported it to C# and improved a bit. I am not going to discuss actual code in this article, it is present in zip file, but only give brief explanation of major properties.

Using the Code

The control is built upon UserControl and provides the following original features:

  • Extra UpDown buttons to control Increment value, which may change between adjustable IncreamentMin and IncrementMax. On every Increment change a position of the digit in the value controlled by Value UpDown is advanced back or forth on one position thus providing quick navigation along wide range numbers. Tooltip gives clue which digital position is currently affected.
  • IncrementVisible property which control whether or not Increment UpDown buttons are visible.
  • ExternalUpdate property which governs if the Value changes instantly or thru external code.
  • Pow2Increment property which, if set, directs the control to generate sequence where next value is twice as high or twice as low as previous one. It can generate any base*2^n series, e.g. useful 1, 2, 4, 8 … n^2 (base = 1). In this mode Increment is controlled by IgSpinEdit itself and thus Increment UpDown buttons are automatically hidden.
  • Flexible visual representation of the Value provided by FormatString property along with ValueAsInt and ValueAsHex properties. ValueAsInt and ValueAsHex do not change internal representation of the value, which is double, but control how value is displayed. For example, to show value as Hex, set ValueAsHex to true and FormatString to “0x{0:Xn}”, where n is number of digits to display. ValueAsHex, if set, also changes Increment multiplier from default 10 to 16 to provide same one digit advance on every hit on Increment UpDown.
  • ValueChanging event which is raised if ExternalUpdate is set to true and value UpDown button has been pressed. The control passes new value to handler without updating its internal state. Handler may cancel update by setting e.Cancel or pass new e.Value to external code which should later update internal value state by assigning to Value property directly.
  • ValueChanged event which is raised when Value has been updated internally.
  • Other properties and events are inherited from UserControl.

    The control does not try to prevent all possible erroneous parameter combinations, so please apply common sense while setting control properties and it will work quite well.

    The Control is accompanied with simple test application showing how to use the control and allowing playing with parameters. The application screenshot is presented at the beggining of the article.

    One important thing to note is that IgSpinEdit implements ISupportInitialize interface to handle correct initialization. Fortunately VS Designer automatically handles calls to ISupportInitialize BeginInit()/EndInit() pair, so you should not do it yourself.

    For test convenience an "External update" handler is implemented in lower left corner of the test application and it handles ValueChanging event of all IgSpinEdit controls. Please note that on application start up only “Value as Int” has ExternalUpdate set what prevents it from changing value until Update button is pressed.

    Conclusion

    This article presents extended numeric UpDown control which is especially useful for hardware control applications. There is nothing fancy in it, but I found it really useful for my projects. I hope some readers will agree with me.
    Feel free to use provided code in any way you wish. Comments and suggestions are welcome.

    History

    06-18-2008:

    • Original article

    License

    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    About the Author

    Igor Voynovskyy


    I hold MSEE from Kiev State University, Kiev, Ukraine.

    At present I am working in Marmara Research Center in Turkey in Joint Research Lab as hardware and software engineer dealing mainly with development of microwave tomography systems.
    Occupation: Systems Engineer
    Company: Marmara Resaerch Center, Turkey
    Location: Ukraine Ukraine

    Other popular Miscellaneous articles:

    Article Top
    Sign Up to vote for this article
    You must Sign In to use this message board.
    FAQ FAQ Noise ToleranceSearch Search Messages 
     Layout  Per page   
      (Refresh) 
    -- There are no messages in this forum --

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    PermaLink | Privacy | Terms of Use
    Last Updated: 18 Jun 2008
    Editor:
    Copyright 2008 by Igor Voynovskyy
    Everything else Copyright © CodeProject, 1999-2009
    Web19 | Advertise on the Code Project