|
-
Sep 15th, 2000, 04:12 AM
#1
Thread Starter
Evil Genius
BACKGROUND : Teaching brother vb, starting with 3 txtboxes on a form simple calculator example (4 buttons add, minus, divide, multiply). type number into txt1 & txt2, hit one of the 4 buttons, hey presto, total in box3.
PROBLEM : 
OK, I use
Code:
txt3.text = txt1.text * txt2.text
which works fine with * & /. With the + & - however I get the following:
- ON the -, no entries appear
- On the +, 2 numbers join, not add (I.e.- 1+1=11, 2+2=22)
Now even I know 2+2 is not 22, this just doesn't add up (pun intended there, sorry, had to put it in). Can anyone shed some light on this please? 
Thank you!
-
Sep 15th, 2000, 04:25 AM
#2
Junior Member
I believe the problem is that you are using strings to do math. What you should do is to put your numbers in variables ex. integer, long etc.
lars
Lars Jarle Mæhlum
http://www.touch-it.no
-
Sep 15th, 2000, 04:25 AM
#3
Addicted Member
Think of the Data Type
Convert the Text into a numeric data type
either Integer, Single, Double, Byte etc..
txt3.text = CInt(txt1.text) * CInt(txt2.text)
The reason for getting 22 and 11 is because it is concatenating two strings
Hope that helps
Steve
-
Sep 15th, 2000, 04:26 AM
#4
Addicted Member
Convert
Convert ! And use the right operators. Don't use & to add because it will concatenate the strings.
Do something like (I didn't test this, wrote it straight into the reply):
Code:
dim dblValue1 as double
dim dblValue2 as double
if IsNumeric(txt1) then
dblValue1 = cdbl(txt1)
if IsNumeric(txt2) then
dblValue2 = cdbl(txt2)
txt3 = cstr(dblValue1 + dblValue2)
Else
msgbox "Non-numeric value
End
Else
msgbox "Non-numeric value
EndIf
Don't rely on VB to convert values properly. It will often go well, but it will go wrong when you least expect it.
Regards,
Laurens
Using VB5 Enterprise edition SP3
VB6 Enterprise edition SP5
-
Sep 15th, 2000, 04:27 AM
#5
Well A + B = AB. When you add text you're concatenatin strings. Textboxes have text in the text property (yeah, no kidding) 
What you want to do is to convert the text into number values.
Code:
txt3.Text = Val(txt1.Text) + Val(txt2.Text)
Good luck!
-
Sep 15th, 2000, 04:31 AM
#6
Wow, I've never seen so many people answering a question at virtually the same time.
Just one comments though;
Don't use CInt if you're not only calculation integers.
-
Sep 15th, 2000, 04:45 AM
#7
Thread Starter
Evil Genius
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
|