|
-
May 10th, 2009, 09:16 PM
#1
Thread Starter
New Member
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!!!
-
May 10th, 2009, 09:26 PM
#2
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 
-
May 10th, 2009, 09:29 PM
#3
Re: Simple calculator, simple problem...
 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!!! 
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.
-
May 10th, 2009, 09:30 PM
#4
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|