Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. subtracting decimal values in textboxes

subtracting decimal values in textboxes

Scheduled Pinned Locked Moved Visual Basic
questionhelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BORN again
    wrote on last edited by
    #1

    Hi everyone, Can someone help me with following code Dim temp1 as decimal dim temp2 as decimal dim result as decimal temp1 = CDec(txtvalue1.text).tostring("N4") temp2 = Cdec(txtvalue2.text).tostring("N4") result = temp1-temp2 txtfinalvalue.text = Cstr(result) what is wrong in the above code? I have decimal values in two textboxes and I need to subtract them and display the result as a decimal value into the third textbox. thankyou all in advance Aartee. ...HE is watching Us All! -- modified at 21:50 Wednesday 14th September, 2005

    S 1 Reply Last reply
    0
    • B BORN again

      Hi everyone, Can someone help me with following code Dim temp1 as decimal dim temp2 as decimal dim result as decimal temp1 = CDec(txtvalue1.text).tostring("N4") temp2 = Cdec(txtvalue2.text).tostring("N4") result = temp1-temp2 txtfinalvalue.text = Cstr(result) what is wrong in the above code? I have decimal values in two textboxes and I need to subtract them and display the result as a decimal value into the third textbox. thankyou all in advance Aartee. ...HE is watching Us All! -- modified at 21:50 Wednesday 14th September, 2005

      S Offline
      S Offline
      Steve Pullan
      wrote on last edited by
      #2

      BORN...again! wrote: temp1 = CDec(txtvalue1.text).tostring("N4") temp2 = Cdec(txtvalue2.text).tostring("N4") r Change this to:

      temp1 = CDec(txtvalue1.text)
      temp2 = Cdec(txtvalue2.text)

      ...Steve

      B 1 Reply Last reply
      0
      • S Steve Pullan

        BORN...again! wrote: temp1 = CDec(txtvalue1.text).tostring("N4") temp2 = Cdec(txtvalue2.text).tostring("N4") r Change this to:

        temp1 = CDec(txtvalue1.text)
        temp2 = Cdec(txtvalue2.text)

        ...Steve

        B Offline
        B Offline
        BORN again
        wrote on last edited by
        #3

        Hi Steve, Thankyou for your prompt help. I tried it like you said but i still get the error as "System.InvalidCastException: Cast from string "" to type 'Decimal' is not valid." Any more tips? thanks a ton Aartee. ...HE is watching Us All!

        D S 2 Replies Last reply
        0
        • B BORN again

          Hi Steve, Thankyou for your prompt help. I tried it like you said but i still get the error as "System.InvalidCastException: Cast from string "" to type 'Decimal' is not valid." Any more tips? thanks a ton Aartee. ...HE is watching Us All!

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          One of those textboxes doesn't have anything in it when this code runs. Put those two CDel lines in seperate Try/Catch blocks to make sure the values are filled in with 0 if the strings enter into the textbox's are not valid numbers.

          Try
          value1 = CDel(textbox1.Text)
          Catch
          value1 = 0
          End Try

          RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          1 Reply Last reply
          0
          • B BORN again

            Hi Steve, Thankyou for your prompt help. I tried it like you said but i still get the error as "System.InvalidCastException: Cast from string "" to type 'Decimal' is not valid." Any more tips? thanks a ton Aartee. ...HE is watching Us All!

            S Offline
            S Offline
            Steve Pullan
            wrote on last edited by
            #5

            Dave is correct. You need to ensure the data being converted is valid before the conversion takes place otherwise an exception can be thrown. ...Steve

            B 1 Reply Last reply
            0
            • S Steve Pullan

              Dave is correct. You need to ensure the data being converted is valid before the conversion takes place otherwise an exception can be thrown. ...Steve

              B Offline
              B Offline
              BORN again
              wrote on last edited by
              #6

              Thanks guys, I initialised the values of the variables and Bingo!thanks for all the help. Aartee. ...HE is watching Us All!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups