Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to Calculate

Posted on 2011-03-14
23
Medium Priority
?
556 Views
Last Modified: 2012-06-27
Hi i want make a calculator, using visual basic 6.0
> 2 textboxes and 1 command button
in first textbox input will be like this  180.25*10-26+400/2  (etc...)
and answer will be on second text box on command button Clicked

How can i do this
please assist me

0
Comment
Question by:crystal_Tech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 10
23 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35130128
0
 
LVL 1

Author Comment

by:crystal_Tech
ID: 35130200
Thanks for reply
But its giving me error
compile error : user defined type not defined
and highlighted on
ScriptEngine As New MSScriptControl.ScriptControlClass



Private Sub Command1_Click()
      Dim ScriptEngine As New MSScriptControl.ScriptControlClass
        ScriptEngine.Language = "VBScript"
        Dim problem As String
        problem = Text1.Text
        Dim answer As String
        answer = ScriptEngine.Eval(problem)
        MessageBox.Show (answer)
End Sub
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35130292
Right...as stated in the linked answer, you need to add a REFERENCE to the Microsoft Script Control 1.0.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:crystal_Tech
ID: 35130302

Sir
I already added
Microsoft Script Control 1.0.

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35130357
*Haven't code in VB6 in literally years...

Try it like this:

    Dim ScriptEngine As New ScriptControl

Or maybe:

    Dim ScriptEngine As ScriptControl
    Set ScriptEngine = New ScriptControl
0
 
LVL 1

Author Comment

by:crystal_Tech
ID: 35130457
Compile Error
Invalid use of new keyword

Highlighted on ScriptEngine As New ScriptControl

and when i am using

Dim ScriptEngine As ScriptControl
    Set ScriptEngine = New ScriptControl

Highlighted on  New ScriptControl

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35130510
You could just add the Script Control directly to your form like any other control:
http://support.microsoft.com/kb/184740
0
 
LVL 1

Author Comment

by:crystal_Tech
ID: 35130633
sir

Getting Run Time Error '424'
Object Required
and on debug highlighted to             ScriptEngine.Language = "VBScript"

Using following code

        With ScriptControl1

            ScriptEngine.Language = "VBScript"
            Dim problem As String
            problem = Text1.Text
            Dim answer As String
            answer = ScriptEngine.Eval(problem)
            MessageBox.Show (answer)
        End With

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35130650
"With ScriptControl1"

Your control is called ScriptControl1.

...so change all "ScriptEngine" to "ScriptControl1".
0
 
LVL 1

Author Comment

by:crystal_Tech
ID: 35130748

Getting Wrong Answer

Private Sub Command1_Click()
Text2.Text = ""
        With ScriptControl1

            ScriptControl1.Language = "VBScript"
            Dim problem As String
            problem = Text1.Text
            Dim answer As String
            answer = ScriptControl1.Eval(problem)
            Text2.Text = answer
        End With
End Sub

input is in text one
150+150+150*3-100
and aswer should be 1250 instead of 650

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35130821
How are you getting 1250?

That would be interpreted as:

    150+150+(150*3)-100
    150+150+450-100
    650

In the absence of parenthesis, Multiplication/Division is always calculated before Addition/Subtraction.
0
 
LVL 1

Author Comment

by:crystal_Tech
ID: 35130854

oh ok but sir
i want it like that
150+150+150*3-100 = 1250
instead of 650
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35130867
then you need to add parenthesis to force the addition to occur before the multiplication:

    (150+150+150)*3-100 = 1250
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35130904
See:
http://msdn.microsoft.com/en-us/library/6s7zy3d1(VS.85).aspx

    "When multiplication and division occur together in an expression, each operation is evaluated as it occurs from left to right. When addition and subtraction occur together in an expression, each operation is evaluated in order of appearance from left to right. Parentheses can be used to override the order of precedence and force some parts of an expression to be evaluated before others. Operations within parentheses are always performed before those outside. Within parentheses, however, operator precedence is maintained."
0
 
LVL 1

Author Comment

by:crystal_Tech
ID: 35130905

Please show me sir how to do that

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35130923
Are you kidding me?.... =\

Place this:

    (150+150+150)*3-100

into your Text1 control and see what the answer is.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35130937
0
 
LVL 1

Author Comment

by:crystal_Tech
ID: 35131047

No Sir I am Not kidding

your solution is absolutely correct
but my requirement is not 650 is 1250
so is there any way to remove operator ( ) and calculate complete line
0
 
LVL 1

Author Comment

by:crystal_Tech
ID: 35131078
i am gonna use only 4 following operators

+  -  *  /


0
 
LVL 101

Expert Comment

by:mlmcc
ID: 35131240
How do you know the answer is 1250?

mlmcc
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35131245
If you want to use a custom precedence for operators (without parenthesis) then you can't use the Script Control.

You'd either have to use a different canned control or write your own lexical parser from scratch.

Can you give more details about how this is going to be used?
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 35131284
For your limited set of operators, and left to right evaluation WITHOUT precedence:
http://www.devx.com/vb2themax/Tip/18651
0
 
LVL 1

Author Comment

by:crystal_Tech
ID: 35131666

Sir Thats it
Word to word

A simple expression evaluator by Francesco Balena

Thank you So much Sir

0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Loops Section Overview
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question