how to get the result

Discussion in 'AutoCAD' started by youngman, Jun 15, 2004.

  1. youngman

    youngman Guest

    hi,
    i add a textbox into a form and input 2+2 in the textbox.

    then i msgbox textbox..value ,2+2 is showed .

    how can i get 4 instead of 2+2

    thank you

    regards
     
    youngman, Jun 15, 2004
    #1
  2. youngman

    Thilak Guest

    You mean that u have entered 2+2 on same text box?, then u need to concat the textbox1.text value for identifying the operator and add the values on either side of the operator and then display in msgbox.

    Other wise, better method would be, using two text box and create a add button, whose event will add the two text box value and display in a msgbox.
     
    Thilak, Jun 15, 2004
    #2
  3. youngman

    youngman Guest

    Thanks for your advice.

    but as a matter of fact, i wand to input more complicated expression. not
    just 2+2

    i have no idea about it.

    thank you again


    the textbox1.text value for identifying the operator and add the values on
    either side of the operator and then display in msgbox.
    button, whose event will add the two text box value and display in a msgbox.
     
    youngman, Jun 15, 2004
    #3
  4. You need make a function to convert the algebraic expression into a vb
    operation
    This is a simple example:

    Private Function SimpleCalc(StrOp As String) As Double
    Dim Operator(3) As String
    Dim Op1 As Double
    Dim Op2 As Double
    Dim pos As Integer
    Dim i As Integer
    Operator(0) = "+"
    Operator(1) = "-"
    Operator(2) = "/"
    Operator(3) = "*"

    If StrOp = "" Then
    SimpleCalc = 0
    Else
    'search for operator
    For i = LBound(Operator) To UBound(Operator)
    pos = InStr(1, StrOp, Operator(i))
    If pos > 0 Then
    Op1 = Val(StrOp)
    Op2 = Val(Right$(StrOp, Len(StrOp) - pos))
    Exit For
    End If
    Next i
    Select Case i
    Case Is = 0
    SimpleCalc = Op1 + Op2
    Case Is = 1
    SimpleCalc = Op1 - Op2

    Case Is = 2
    If Op2 = 0 Then
    MsgBox "Invalid operand"
    SimpleCalc = 0
    Else
    SimpleCalc = Op1 / Op2
    End If

    Case Is = 3
    SimpleCalc = Op1 * Op2

    Case Else
    MsgBox "Unknown operator"
    SimpleCalc = 0
    End Select
    End If

    End Function
     
    Jorge Jimenez, Jun 15, 2004
    #4
  5. youngman

    youngman Guest

    thank you

    this is a good idea,
    it looks like that there isnot a easy way to solve the problem.

    thanks
     
    youngman, Jun 15, 2004
    #5
  6. youngman

    Joe Sutphin Guest

    This can be quite complex but you will want to do most of your work in the
    KeyPress event of the TextBox using a Select Case statement.

    You will need to handle the display of numbers vs. operators and updating
    the value shown. Don't forget about handling invalid characters as well.

    Joe
    --
     
    Joe Sutphin, Jun 15, 2004
    #6
  7. youngman

    youngman Guest

    Thanks for your advice.

    could you tell something more concretely

    thank you
     
    youngman, Jun 15, 2004
    #7
  8. Youngman,
    Unfortunately, writing your own calculator routine, especially one that
    allows more complicated functions than (+-*/), is not a simple task. I
    think Joe was trying to say this by listing some of the speed bumps you will
    have to make your code handle.

    It would be nice if the Windows calculator would let you reference it and
    send keystrokes or strings to be evaluated, but I don't know if it will. I
    couldn't find it in the References list, so AFAIK you can't do this. Maybe
    there's some shareware around that lets you do this...??

    I think the quickest way to get to a robust calculator routine would be to
    send strings to a Microsoft Excel object. Set a cell's formula equal to the
    string (adding an "=" in front if necessary) and then read the cell's value.
    If it's "#VALUE" or "#-anything" you have an error in the formula --
    otherwise it's your calculator answer. You can find code snippets for
    getting or creating an Excel object, and manipulating a cell by searching
    the NG's.

    If you want it to act like a calculator and have it know when to update the
    value (depending on order of operators, parentheses, etc) then you need code
    to track the current level of parenthesis, current list of operations to be
    done (I think the real programmers would call this a stack and would have
    code at their fingertips to manipulate it, but I don't). Otherwise just let
    the user build a string up and whenever he hits Enter it gets sent through
    Excel.

    This is my best idea at the moment. Hope this helps,

    James
     
    James Belshan, Jun 15, 2004
    #8
  9. What you need is the railroad shunting algorithm.
    http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Stack/

    A google search shows many many hits on this topic. The above is one that I
    found easy to follow.

    I believe that Frank O. has posted a working VB class module in the group
    before...or maybe it was just posted on the site...I don't remember. Maybe
    he'll post it again if we all ask nicely :)
     
    Bobby C. Jones, Jun 15, 2004
    #9
  10. youngman

    youngman Guest

    thanks to you all

    it looks like complicadted ,i have to think out another idea.
    thanks
     
    youngman, Jun 16, 2004
    #10
  11. youngman

    wivory Guest

    Maybe you could achieve something using the Eval method.

    Regards

    Wayne Ivory
    IT Analyst Programmer
    Wespine Industries Pty Ltd
     
    wivory, Jun 16, 2004
    #11
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.