Click here to Skip to main content
Click here to Skip to main content
Go to top

A NumberPicker Control

, 24 Jan 2006
Rate this:
Please Sign up or sign in to vote.
This article presents a control that allows for easy number entering.

NumberPicker control

Introduction

A large number of applications nowadays contain some controls which are used for entering numbers. In particular, in financial applications, this kind of controls are used extensively. A number of other articles have discussed ways in which numbers can be represented more nicely in textbox controls. However, there is still a need for a control that allows the user to very quickly enter numbers. In this article, I will explain the NumberPicker control which will resolve this problem and allows for fast number entering.

This article is structured as follows. First, an explanation is given on the workings of this control and how it was implemented. Afterwards, I will go into the details of how the code can be incorporated in your current projects. Finally, some conclusive remarks are stated as well as the revision history.

Details of the control

This control provides a way in which numbers can be entered by clicking the digits on a dialog-based keypad, as you can see on the screenshot above. When the user double-clicks the control, a small numerical keypad is shown. This keypad allows you to enter numbers in the control. Furthermore, it contains some basic functionality to make simple mathematical calculations on the fly. The operators 'add', 'subtract', 'multiply' and 'divide' are supported. The mechanism to make the calculations is done with delegates.

private delegate double Function(double a, double b);
private Function f;

When one of these operators are clicked, the corresponding function is being held in a delegate f. In this case, the 'Add' operator was pressed.

this.f = new Function(Utilities.Add);

Afterwards, the result of applying the operator to the first entered value (temporary) and the last entered value (value) is calculated by invoking the function contained in the delegate on the two numbers. The result is saved into the temporary again, waiting for any following calculation.

this.temporary = f(this.temporary, Convert.ToDouble(this.value.Text));
this.value.Text = this.temporary.ToString();

The source for the NumberPicker control can be downloaded from the top of this article.

Using the code

Once we have successfully built the solution, the control can be used in other projects as well. This can be done by selecting "Add/Remove Items.." from the toolbox context menu and locating the "CustomControlLibrary.dll". Once the control is added to the toolbox, we can simply draw it on our forms and use it like any other control.

Textually, one could also write the following to use the control:

this.numberPicker = new CustomControlLibrary.NumberPicker();
this.numberPicker.Location = new System.Drawing.Point(0, 0);
this.numberPicker.MaxLength = 32767;
this.numberPicker.Modified = false;
this.numberPicker.Name = "myNumberPicker";
this.numberPicker.Size = new System.Drawing.Size(100, 20);

A small demo of the NumberPicker available for download from the top of this article.

Conclusions

The control that was described here can be used in applications that require fast number entering (e.g. financial applications). It provides a dialog-based keypad when double-clicking, and via simple mouse clicks, numbers can be entered in to the control.

As I have said in the introduction, other people have done work on representing numbers more nicely in a textbox. This control could very well be used with these other edit controls to obtain an even richer editing of numeric values (e.g. visualizing the numbers in red when they are negative, etc.).

History

  • Dec 09, 2005 - Version 1.0.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Bram Pellens
Researcher
Belgium Belgium
No Biography provided

Comments and Discussions

 
GeneralGreat Tool Pinmembergood_raghu_m198-Oct-08 5:18 
GeneralI have a little input for you! PinmemberMartin#24-Jan-07 20:05 
QuestionNice Control - New Version? PinmemberArnold Terrence Smith6-Oct-06 4:55 
GeneralSome little advices [modified] PinmemberBoyko Markov2-Aug-06 6:11 
GeneralDatabinding doesn't work PinmemberTommi0078-Jun-06 21:26 
GeneralBug of the Control PinmemberWuBill5-May-06 1:31 
GeneralRe: Bug of the Control PinmemberBram Pellens16-May-06 21:50 
GeneralGreat control, just what I was looking for! Pinmemberdzefting12-Dec-05 15:27 
GeneralA globalization comment Pinmembernadav7410-Dec-05 0:55 
GeneralRe: A globalization comment PinmemberBram Pellens10-Dec-05 13:20 
GeneralMotivation for the control Pinmembermav.northwind9-Dec-05 20:12 
GeneralRe: Motivation for the control Pinmember.rich.w10-Dec-05 10:28 
GeneralRe: Motivation for the control PinmemberBram Pellens10-Dec-05 13:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 24 Jan 2006
Article Copyright 2005 by Bram Pellens
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid