• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 455
  • Last Modified:

VB 2008: Calculate mathematical string from textbox

I have a form with a textbox. Users can enter any mathematical string.

How do I get vb to calculate any mathematical string and spit it out in a msgbox.

i.e.1. if the user enters 23+34/2, a messagebox will popup stating: "40":
i.e.2. if the user enters 1+5/5-6+2-1 a messagebox will popup stating: "-3"

Visual Basic 2008 does not seem to have a "EVAL()" function.


0
ouestque
Asked:
ouestque
  • 2
3 Solutions
 
jpaulinoCommented:
Check this example of an expression evaluator
http://www.codeproject.com/KB/vb/expression_evaluator.aspx 
0
 
game-masterCommented:

GOOD AFTERNOON

Click on Project --> Add Reference.
Switch to the COM tab.
Select the "Microsoft Script Control 1.0" entry and click OK.

Then use code like this:

        Dim ScriptEngine As New MSScriptControl.ScriptControlClass
        ScriptEngine.Language = "VBScript"
        Dim problem As String = "(150 + 20) * 2 / (7 - 4)"
        Dim answer As String = ScriptEngine.Eval(problem)
        MessageBox.Show(answer)


I HOPE I COULD GIVE U IDEA..


game-master
0
 
game-masterCommented:


change the "MSScriptControl.ScriptControlClass" to "MSScriptControl.ScriptControl"

it works fine...

game-master
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now