Results 1 to 4 of 4

Thread: Simple calculator, simple problem...

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    13

    Simple calculator, simple problem...

    im trying to make a very siple calculator (to the point of only for lines of code) however the addtion dosnt work:

    this is all my code:
    Code:
    Private Sub add_Click()
    Text3.Text = Text1.Text + Text2.Text
    End Sub
    
    Private Sub divide_Click()
    Text3.Text = Text1.Text / Text2.Text
    End Sub
    
    Private Sub multiply_Click()
    Text3.Text = Text1.Text * Text2.Text
    End Sub
    
    Private Sub subtract_Click()
    Text3.Text = Text1.Text - Text2.Text
    End Sub
    this is the code that isnt working even though it clearly should be:
    Code:
    Private Sub add_Click()
    Text3.Text = Text1.Text + Text2.Text
    End Sub
    when i test it, i input 4 + 4 and it gives me an aswer such as 44.
    why not eight? god visual basic is makeing me very angry!!!

  2. #2
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Simple calculator, simple problem...

    data enter into textbox's is data type string
    try this
    Private Sub add_Click()
    Text3.Text = Val(Text1.Text) + Val(Text2.Text)
    End Su
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Simple calculator, simple problem...

    Quote Originally Posted by xxlethargyxx View Post
    ...when i test it, i input 4 + 4 and it gives me an aswer such as 44.
    why not eight? god visual basic is makeing me very angry!!!
    Because you are concatenating strings (Text1,text and Text2.Text).
    What you need instead is convert each string value to numeric type using any available conversion function (CInt, CLng, CSng, CDbl, etc) or at least use Val function:

    Text3.Text = Val(Text1.Text) + Val(Text2.Text)

    Best approcah however is to disallow nonnumeric input to begin with.
    MartinLiss has nicely done NumberTextbox control so take a look at it.

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    13

    Re: Simple calculator, simple problem...

    YESSS!!!! thank you very much!!!!

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