Adding Currency with Text Boxes

  • 19 years ago

    I'm working on a game inwhich I'm using text boxes to keep track of money.  When I try to add or subtract from them it is adding them together like a string.


    Ex.,  Private Sub CmndStash_Click()
             frmMainGame.TextCash.Text = (frmMainGame.TextCash.Text -                   Me.TxtStash.Text)
             frmMainGame.TextStashedCash.Text =                              (frmMainGame.TextStashedCash.Text +Me.TxtStash.Text)
             Unload Me
           End Sub


    For some reason when I add the money together it gets results like this:


     frmMainGame.TextStashedCash.Text = $200
     frmMainGame.TextCash.Text = $150
     Adding them both using the procedure listed above equals $200,$150
     Or sometimes when I change the formating it equals $200,150



    Any help out there?

  • 19 years ago

    What you need to do is dim variables as Currency, set the variables equal to the text box values, respectively, and THEN add them together.  then set the textbox equal to the sum.  use Format(cur1, "$#.00") to ensure you only get two decimal places.  when you add strings together, it's the same as using "&" and when you do it with a variant, if the variant IsNumeric, then when you use the plus sign it adds like numbers and when you use the ampersand (&), it adds like strings.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“It works on my machine.” - Anonymous