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!!!:mad:
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
Re: Simple calculator, simple problem...
Quote:
Originally Posted by
xxlethargyxx
...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!!!:mad:
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.
Re: Simple calculator, simple problem...
YESSS!!!! thank you very much!!!!