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

Numeric TextBox : Allow your users to enter numeric data the easy way

Rate me:
Please Sign up or sign in to vote.
4.38/5 (25 votes)
20 Jun 20041 min read 462.8K   5.4K   56   44
A TextBox control for numeric input, with the ability to set precision, scale (when focused or not)

Introduction

Numeric input has always been one of the key-features in many applications (CRM, Accounting,...). Although MS provides a masked edit control, many developers find it to have some limitations.

The Numeric TextBox tries to provide some features to allow the user to input numeric data in a natural way. Let's have a look at some of the key features.

  1. Allow/Disallow negative input.
  2. Specify the maximum precision and scale.
  3. Specify a different scale when the control hasn't got the focus.
  4. Allow/Disallow zero as a valid input.
  5. The control displays grouping characters.
  6. The control adapts the regional settings (grouping char, decimal char).

Using the code

To use the control simply reference the Shorty.Windows.Forms assembly in your project. Create (or drag and drop on the form) a NumericTextBox and set the properties. The properties can be set trough code or via the property pane (Numeric settings section). See below for an example.

C#
// 
// numericTextBox1
// 
Shorty.Windows.Forms.NumericTextBox() numericTextBox1 = 
  new Shorty.Windows.Forms.NumericTextBox();

this.numericTextBox1.AllowNegative = true;
this.numericTextBox1.DataBindings.Add(
  new System.Windows.Forms.Binding("NumericValue", 
  this, "Table1.test"));
this.numericTextBox1.NumericPrecision = 8;
this.numericTextBox1.NumericScaleOnFocus = 5;
this.numericTextBox1.NumericScaleOnLostFocus = 3;
this.numericTextBox1.NumericValue = 0;
this.numericTextBox1.ZeroIsValid = false;

The control automatically checks if the combination NumericPrecision NumericScaleOn(Lost)Focus is valid. As you can see in the above sample to retrieve the numeric value displayed in the NumericTextBox you should use the NumericValue property. The control is also capable of databinding it's NumericValue property. (See above code sample).

History

  • Initial release version 0.1.0.0 (20/06/2004)

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
Web Developer
Belgium Belgium
3 years ago I started out with VB 6 and ASP. After seeing .Net in Beta in 2001 I haven't done anything else. I moved from VB6 over VB.Net to C#.
In 2004 I earned my MCAD. Currently I am employed as a Analyst/Developer and I'm working towards my MCDBA (1 exam to go Smile | :) ).

You can follow me at my blog http://radio.weblogs.com/0132279/

Comments and Discussions

 
GeneralDrop Control on to form Pin
Neil Squires19-Jul-07 7:01
Neil Squires19-Jul-07 7:01 
AnswerRe: Drop Control on to form Pin
hellspawnfr1-Feb-08 22:28
hellspawnfr1-Feb-08 22:28 
GeneralRe: Drop Control on to form Pin
Mahmood Dehghan28-Mar-08 23:08
Mahmood Dehghan28-Mar-08 23:08 
GeneralException Pin
naalexander27-Jul-06 3:12
naalexander27-Jul-06 3:12 
GeneralRe: Exception Pin
Carlo Bos11-Jan-07 8:11
Carlo Bos11-Jan-07 8:11 
GeneralRe: Exception Pin
richm12234513234128-Apr-07 21:49
richm12234513234128-Apr-07 21:49 
GeneralThanks, very helpful . Pin
Roman Lerman23-Jul-06 0:54
Roman Lerman23-Jul-06 0:54 
GeneralWhat's about Cut, Copy and Paste Pin
carloqueirolo22-Jul-06 7:28
carloqueirolo22-Jul-06 7:28 
That's an awesome control.
But I noted that the control does not accept Ctl-C, Ctrl-V and Ctrl-X. Instead, right-click context menu (Cut, Copy and Paste) is regulary accepted.
Is there a fix for this?
Thank you.
c
GeneralRe: What's about Cut, Copy and Paste Pin
Ciupaz12-Apr-11 0:11
Ciupaz12-Apr-11 0:11 
Generalmy numtextbox control Pin
byshome23-Mar-06 17:04
byshome23-Mar-06 17:04 
Generalbug II Pin
lethedon1-Mar-06 5:15
lethedon1-Mar-06 5:15 
GeneralVery good numeric textbox - simple and efficient Pin
clody5-Dec-05 23:44
clody5-Dec-05 23:44 
GeneralRe: Very good numeric textbox - simple and efficient Pin
drjoju15-Mar-06 9:20
drjoju15-Mar-06 9:20 
GeneralExcellent Pin
Kia Rahimzadeh26-Oct-04 23:02
Kia Rahimzadeh26-Oct-04 23:02 
GeneralCursor position goes wrong when the comma appears Pin
Thuc.Nguyen7-Sep-04 17:43
professionalThuc.Nguyen7-Sep-04 17:43 
GeneralRe: Cursor position goes wrong when the comma appears Pin
asterixnz19-Sep-06 16:18
asterixnz19-Sep-06 16:18 
GeneralRe: Cursor position goes wrong when the comma appears Pin
R. van der Pal17-Dec-06 12:10
R. van der Pal17-Dec-06 12:10 
GeneralNice! Pin
Marc Clifton22-Aug-04 11:40
mvaMarc Clifton22-Aug-04 11:40 
GeneralRe: Nice! Pin
Hannes Decorte22-Aug-04 19:01
Hannes Decorte22-Aug-04 19:01 
Questionanother one used in web ? Pin
´ó±Ç×Ó¹·27-Jun-04 16:11
´ó±Ç×Ó¹·27-Jun-04 16:11 
GeneralComponent not in Toolbox Pin
adwilcox25-Jun-04 10:44
adwilcox25-Jun-04 10:44 
Questionbug?? Pin
Selvin25-Jun-04 8:18
Selvin25-Jun-04 8:18 
AnswerRe: bug?? Pin
Hannes Decorte26-Jun-04 0:55
Hannes Decorte26-Jun-04 0:55 

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.