Click here to Skip to main content
15,885,032 members
Articles / Programming Languages / C#
Article

A NumberPicker Control

Rate me:
Please Sign up or sign in to vote.
4.39/5 (8 votes)
24 Jan 20063 min read 80.4K   1.1K   58   13
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.

C#
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.

C#
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.

C#
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:

C#
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


Written By
Researcher
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGreat Tool Pin
good_raghu_m198-Oct-08 5:18
good_raghu_m198-Oct-08 5:18 
GeneralI have a little input for you! Pin
Martin#24-Jan-07 20:05
Martin#24-Jan-07 20:05 
QuestionNice Control - New Version? Pin
Arnold Terrence Smith6-Oct-06 4:55
Arnold Terrence Smith6-Oct-06 4:55 
GeneralSome little advices [modified] Pin
Boyko Markov2-Aug-06 6:11
Boyko Markov2-Aug-06 6:11 
GeneralDatabinding doesn't work Pin
Tommi0078-Jun-06 21:26
Tommi0078-Jun-06 21:26 
GeneralBug of the Control Pin
WuBill5-May-06 1:31
WuBill5-May-06 1:31 
GeneralRe: Bug of the Control Pin
Bram Pellens16-May-06 21:50
Bram Pellens16-May-06 21:50 
GeneralGreat control, just what I was looking for! Pin
dzefting12-Dec-05 15:27
dzefting12-Dec-05 15:27 
GeneralA globalization comment Pin
nadav7410-Dec-05 0:55
nadav7410-Dec-05 0:55 
GeneralRe: A globalization comment Pin
Bram Pellens10-Dec-05 13:20
Bram Pellens10-Dec-05 13:20 
GeneralMotivation for the control Pin
mav.northwind9-Dec-05 20:12
mav.northwind9-Dec-05 20:12 
Hi!
You write this control allows fast entering of numbers, but didn't say why anybody should favour the control over simply using the keyboard. Typing the numbers is always faster than anything mouse-related you can come up with...

So I think the important point is that this control only is useful if you don't have a keyboard (e.g. in a kiosk environment with touch screen).

Regards,
mav
GeneralRe: Motivation for the control Pin
.rich.w10-Dec-05 10:28
.rich.w10-Dec-05 10:28 
GeneralRe: Motivation for the control Pin
Bram Pellens10-Dec-05 13:32
Bram Pellens10-Dec-05 13:32 

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

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