Howto consider a TextBox's text as a mathematical calculation

I'm a beginner of VB so please someone help me with this.

I'd like to make a calculator that has a TextBox control and for example if I write in 2+2-5/6

Then it should return the result in the same textbox replacing the calculation if I press the "=" button

My problem is that I can't get the input text to be considered as a mathematical calculation...

Please help!

[387 byte] By [Greatestevil] at [2007-12-24]
# 1

Greatestevil,

The easiest way is to create an integer variable then perform the calculations with the variable and then display the results in the textbox.

The following code will show an example of this; you would place this into your "=" button.click event area...

Dim intCalc As Integer
intCalc = 2 + 2 - 5 / 6
TextBox1.Text = intCalc

Now to make it a little more simple you can have the intCalc variable equal your textbox.text. The only thing to watch out for here is that the textbox can contain strings and the intCalc would not be able hold anything other than a number. But there are many ways around this. The nice thing about programming is the ability to use your imagination to solve the problem or to "re-invent the wheel".

Welcome to programming; enjoy!

I hope the above helps!

Thank you,

James

ReaSoftwareEngineering at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2
Rea Software Engineering wrote:

Greatestevil,

The easiest way is to create an integer variable then perform the calculations with the variable and then display the results in the textbox.

The following code will show an example of this; you would place this into your "=" button.click event area...

Dim intCalc As Integer
intCalc = 2 + 2 - 5 / 6
TextBox1.Text = intCalc

Now to make it a little more simple you can have the intCalc variable equal your textbox.text. The only thing to watch out for here is that the textbox can contain strings and the intCalc would not be able hold anything other than a number. But there are many ways around this. The nice thing about programming is the ability to use your imagination to solve the problem or to "re-invent the wheel".

Welcome to programming; enjoy!

I hope the above helps!

Thank you,

James

First I need to correct the suggestion from using an integer to using Double. The integer will only hold whole numbers. Such as your example should result in a 3.166667 and if you use an integer it will round down to show 3; the datatype "Double" will show the decimal places. See the MSDN documentation for more information on datatypes to best decide what suits you. The coding should look like this:

Dim dblCalc As Double
dblCalc = 2 + 2 - 5 / 6
TextBox1.Text = dblCalc

Another thing to point out is that VB performs mathematical equations in the logic of a mathematician; I believe this is called the logical order, or something like that...it's been way to long since I have dealt with math equations

The point being is that the example equation will equal 3.16667 because of the order of operations (multiply, divide, add, subtract); 5/6 is performed before 2+2 and then 5/6 is subtracted from 4 (result of 2+2).

If you want to bypass this you would do it the same way a mathematician would; by using parenthesis to determine the operation order. I.E. (2+2-5) / 6 = -0.166667.

I hope this additional information will help clear out some missed points and provide you with the right path to completing your project.

Good Luck!

Thank you,

James

ReaSoftwareEngineering at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
You need an expression parser and evaluator, a pretty complicated chunk of code that is best borrowed...
nobugz at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4
Thank you all for helping me out, I learned much from these...
I never seen such a fast and useful reaction to my posts ever on any forum...
I'll keep on working...
Greatestevil at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...