Click here to Skip to main content
11,647,170 members (71,084 online)
Click here to Skip to main content

NumericTextBox (C#.NET)

, 7 Dec 2011 CPOL 44.4K 1 19
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.

Update 

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

NumericTextBox 

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>
</span>

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>
</span>
Thank you for reading! Any problems, ideas, help, constructive criticisms are welcome in the comments.

License

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

Share

About the Author

Akos Orban
Student
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 22:36
memberTimmeey30-Dec-14 22:36 
GeneralMy vote of 5 Pin
Paul Boothroyd14-Dec-11 3:12
memberPaul Boothroyd14-Dec-11 3:12 
GeneralGood work Orban Pin
sudhansu_k1237-Dec-11 19:31
membersudhansu_k1237-Dec-11 19:31 
GeneralMy vote of 5 Pin
Kurniawan Prasetyo7-Dec-11 0:09
memberKurniawan Prasetyo7-Dec-11 0:09 
AnswerRe: My vote of 5 Pin
Akos Orban7-Dec-11 1:23
memberAkos Orban7-Dec-11 1:23 
GeneralMy vote of 5 Pin
Kanasz Robert29-Nov-11 22:38
memberKanasz Robert29-Nov-11 22:38 
QuestionVisualizing the result Pin
VallarasuS27-Nov-11 2:57
memberVallarasuS27-Nov-11 2:57 
AnswerRe: Visualizing the result Pin
Akos Orban27-Nov-11 4:29
memberAkos Orban27-Nov-11 4:29 

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 | Terms of Use | Mobile
Web04 | 2.8.150804.2 | Last Updated 7 Dec 2011
Article Copyright 2011 by Akos Orban
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid