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.
Maximum properties: The
Value has to be between these limits, if it would be outside the interval, it will be set to the
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:
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
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:
int AutoValidationTime To set the validation time in milliseconds. Default:
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):
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:
Thank you for reading! Any problems, ideas, help, constructive criticisms are welcome in the comments.