Click here to Skip to main content
12,747,366 members (35,243 online)
Click here to Skip to main content
Add your own
alternative version


1 download
20 bookmarked
Posted 26 Nov 2011

NumericTextBox (C#.NET)

, 7 Dec 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
A TextBox for numbers with built-in mathematical expression evaluation

You can use the code and the compiled DLLs for any purpose, however if possible please mark the source.


The article was updated to the version 1.2. The links are already pointing to the new versions of the files. See details below. 


In my project, I had a TextBox on one of the forms for entering quantity, and I thought it would be a cool feature if it could understand such basic expressions as 10+42 or 4*8 and so on...

So I decided to create a new Control derived from the TextBox, because this could be useful in the future, and now I'm sharing my result.

First of all, since I decided to create a new control, I wanted to make it as flexible as possible, so the expressions that can be understood by the control can include the four basic operations (+, -, *, /) and parentheses, in any valid combination (spaces can be used, they will be skipped).
e.g.: 40+2 or (12+3/ 4) * 4-3*3 or 10+2*(2+5* (4-(2-0.5))+1.5)

Second, I added some extra functionality to the TextBox, some are taken from NumericUpDown control, but there are some which I used already but now I built them in the control (changing background color based on value).

Functionality based on NumericUpDown Control

  • Value property (also the Text property is still available)
  • ValueChanged event: Use this instead of the TextChanged event if you want to use the value of the expression, since the Value will only change when the expression in the text box could be evaluated (mostly those can't which are in the middle of editing: e.g. 1+2* ), while the Text changes with each character change.
  • Minimum/Maximum properties: The Value has to be between these limits, if it would be outside the interval, it will be set to the Minimum or Maximum value.
  • DecimalPlaces property
  • InterceptArrowKeys property
  • Increment property
  • ThousandsSeparator property

Extra Functionality

A warning and error value can be set, and if the Value will exceed these values, then the background color of the control will be changed accordingly.

Note: If the error value is exceeded, then the error color will be used, otherwise if the value is over the warning level then the warning color, if it's lower then it will be white.

The properties associated with this feature:

  • bool EnableWarningValue
  • bool EnableErrorValue
  • decimal WarningValue
  • decimal ErrorValue
  • Color WarningColor
  • Color ErrorColor 

New functionality in version 1.1  

As VallarasuS suggested I added the feature that if the Value changed the expression will be automatically validated after a given time, meaning instead of 1+2 it will show in the textbox 3.

This is achieved by adding an event handler for the ValueChanged event, and starting a timer. When the timer ticks the Validate() method is called, and it updates the Text of the TextBox

Note: Also a bug is corrected, which caused the ValueChanged event fire every time when the Text was changed.  

For this there are two new properties:

  • bool AutoValidate To turn on or off this feature. Default:true
  • int AutoValidationTime To set the validation time in milliseconds. Default:5000

I guess the usage is quite straight forward, so I wouldn't spend more time explaining it, let's see the code (v1.2, the code only changed since v1.1 to work with the new version of MathsEvaluator, which has minor differences in method names): 

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing; 
using Rankep.MathsEvaluator;

namespace Rankep.NumericTextBox
    /// <span class="code-SummaryComment"><summary>

The code is using the static methods of my MathsEvaluator class, which I have written for this, and I'm sharing it together with this control.

Changes in v1.2  

The code of this has changed in v1.2, mostly small maintenance and a bug is corrected which caused false evaluation of expressions containing brackets inside brackets. Also a new operator (^ aka power) is supported, and the multiplication sign can be omitted next to a bracket (e.g. 2(3+4)5 is the same as 2*(3+4)*5 or (1+1)(1+1) is the same as (1+1)*(1+1)). 

Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Rankep.MathsEvaluator
    /// <span class="code-SummaryComment"><summary>
Thank you for reading! Any problems, ideas, help, constructive criticisms are welcome in the comments.


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


About the Author

Akos Orban
Poland Poland
No Biography provided

You may also be interested in...


Comments and Discussions

BugWrong output when leaving an empty text field with thousands separator on Pin
Timmeey30-Dec-14 23:36
memberTimmeey30-Dec-14 23:36 
GeneralMy vote of 5 Pin
Paul Boothroyd14-Dec-11 4:12
memberPaul Boothroyd14-Dec-11 4:12 
GeneralGood work Orban Pin
sudhansu_k1237-Dec-11 20:31
membersudhansu_k1237-Dec-11 20:31 
GeneralMy vote of 5 Pin
Kurniawan Prasetyo7-Dec-11 1:09
memberKurniawan Prasetyo7-Dec-11 1:09 
AnswerRe: My vote of 5 Pin
Akos Orban7-Dec-11 2:23
memberAkos Orban7-Dec-11 2:23 
GeneralMy vote of 5 Pin
Kanasz Robert29-Nov-11 23:38
memberKanasz Robert29-Nov-11 23:38 
QuestionVisualizing the result Pin
VallarasuS27-Nov-11 3:57
memberVallarasuS27-Nov-11 3:57 
AnswerRe: Visualizing the result Pin
Akos Orban27-Nov-11 5:29
memberAkos Orban27-Nov-11 5:29 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170215.1 | Last Updated 7 Dec 2011
Article Copyright 2011 by Akos Orban
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid