CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Executing code

  1. #1
    Join Date
    Aug 1999
    Location
    US, Florida
    Posts
    817

    Executing code

    I have "1+1" in my text1, how to make text1 = "2" now? I tried cint(text1), type mismatch. Of course, I can get all the integers using instr and add them using standard variables, but is there anyway to execute the code in textbox? Example: if I have "10-4+6" it would give me "12", or if I have "10/5+5" it would give me "7". There has got to be a function to perform this task. Thanks.

  2. #2
    Join Date
    Dec 2001
    Location
    India
    Posts
    42
    Regards

    Debashish Chakrabarty
    SCJP

    ******************************
    Cool Site for SCJP aspirants at:
    http://www.geocities.com/wahjava
    ******************************

  3. #3
    Join Date
    Aug 1999
    Location
    US, Florida
    Posts
    817
    Is there anyway to do this without a control?

  4. #4
    Join Date
    Feb 2001
    Location
    PA
    Posts
    163
    Text1.Text = 10 - 4 + 6
    If Text1.Text = "12" Then
    MsgBox "Text1 = 12"
    End If
    Text1.Text = 10 / 5 + 5
    If Text1.Text = "7" Then
    MsgBox "Text1 = 7"
    End If
    End Sub

    Cubbie

  5. #5
    Join Date
    Apr 2002
    Location
    Melbourne, Victoria, Australia
    Posts
    1,792
    Haven't got time to code it all up for you, but I'd use something like this:

    Grab string from textbox

    Create array of special characters ( * / + - () ) etc

    Begin Loop
    Check first character of textbox, if its numeric or - (for negative numbers) keep checking until you hit one of the special characters. When you hit that character, convert the previous x characters into a double, and store in an array.

    Store the next character (the operator) in another array and keep checking the string until you get to the next special character or the end of the string
    End Loop

    This is where it will be slightly more difficult.....trying to work out your precedence.

    Begin Loop
    Run through your Operator array looking for your first operation (ie brackets) Store that index in nDx(0). Store the length in characters of the NUMBER (nDxLen(0)). If the first operator isn't found, then run through again looking for the second operator(s) ( * or / ) etc etc.
    End Loop

    Now you start to do the calculations.

    Begin Loop
    Get the index number of your first operator in its own array, take x from that number to get the index (where x is stored in nDxLen(0). Get the first operator (unless the first operator is a "(") and search through for the second number. etc etc
    End Loop

    Apologies if this isn't too clear - I'm in a little bit of a hurry today but I hope this gives you an idea.

  6. #6
    Join Date
    Aug 1999
    Location
    US, Florida
    Posts
    817
    Yeah, I was thinking about seeking out math operators and do the numbers in between, but that's to much hassle, and since I'm already using windows common controls, I need to create a setup program anyway, so I'll just add the script control. Thanks for you help tho.

  7. #7
    Join Date
    Apr 2002
    Location
    Belgium
    Posts
    125
    Just do this

    text1.text = 10 - 4 + 6
    'then the text1.text will be 12
    'you can use the - + / *
    'when you want 11 in your textboxdo
    text1.text = "1" & "1" 'Then he will set the numbers behind eachother and not count them up
    Bert Willekens,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured