|
-
October 21st, 2001, 09:23 PM
#1
Adding Numbers from a textbox
Hello,
I need some help one adding some numbers in a textbox.
Example:
12 2 14 (these well be in the text1 box)
I need to add these numbers up and put them into another textbox.
Text1 = Number
Text2 = Total
------
This is in VB 6.0
-
October 21st, 2001, 09:55 PM
#2
Registered User
Does it have to be a text box or can you enter the number in by an input box?
Other than that what you could do is have the program read both text boxes add the sum up of them together and palce the new sum into the text2 box.
-
October 22nd, 2001, 02:48 AM
#3
If the numbers in text box 1 are seperated by spaces, you can use the "split" function to get the individual numbers. This will put them into an array, which you can then go through and add all the numbers together:
<code>
iArray = Split(txtText1.value, " ")
for each iNum in iArray
iTotal = iTotal + iNum
next
txtText2.value = iNum
</code>
HTH.
-
October 22nd, 2001, 08:49 AM
#4
ahhhhh
Ok.
Yeah I have it only in a text box and there are spaces between each number.
The split may work ill try it after work thank you
-
October 23rd, 2001, 07:57 PM
#5
[quote]Originally posted by antonye:
<strong>If the numbers in text box 1 are seperated by spaces, you can use the "split" function to get the individual numbers. This will put them into an array, which you can then go through and add all the numbers together:
<code>
iArray = Split(txtText1.value, " ")
for each iNum in iArray
iTotal = iTotal + iNum
next
txtText2.value = iNum
</code>
HTH.</strong><hr></blockquote>
humm Im trying to get this to work.
<code>
iArray = Split(Text1.Text, " ")
For Each iNum In iArray
iTotal = iTotal + iNum
Next
Text2.Text = iNum
</code>
It will not add the numbers just leave text2 blank
I run iNum through a msgbox and it ='s nothing. But if I run iTotal it comes up with the numbers without the spaces.
http://www.amd.com/us-en/assets/cont...uts/athlon.gif
Do you leak any fluids, do you have any bumps on your rear, do you have any unpleasant odors?
If you answered yes to any of these then you are not qualified to own a new AMD ATHLON XP.
If someone ask you if you are running the all-new fastest AMD ATHLON XP, just turn around and reply "WHY YES, AND ITS CERTIFIED"
http://www.daemonprojects.com/
-
October 24th, 2001, 03:09 AM
#6
[code]...
Text2.Text = iNum
****
</pre><hr></blockquote>
That should, of course, been iTotal, the total value of the added numbers!
Just wanted to see if you were paying attention and understood what the routine was doing
HTH,
-
October 24th, 2001, 08:33 AM
#7
[quote]Originally posted by antonye:
<strong>[code]...
Text2.Text = iNum
****
</pre><hr></blockquote>
That should, of course, been iTotal, the total value of the added numbers!
Just wanted to see if you were paying attention and understood what the routine was doing
HTH,</strong><hr></blockquote>
hehe, yeah i tried that but it wont add the numbers only take the spaces from between the numbers out then place the value in test2
ex:
text1.text = 1 2 34
text2.text = 1234
--
thats what it does, it wont add the numbers
-
October 24th, 2001, 08:39 AM
#8
[quote]Originally posted by Daemon:
hehe, yeah i tried that but it wont add the numbers only take the spaces from between the numbers out then place the value in test2
<hr></blockquote>
Ok, then it is doing a string concatenation rather than a numeric addition.
Have you DIM'ed your values correctly? These should be:
Dim iArray as long
Dim iNum as variant
Dim iTotal as long
This will then give you an implicit conversion to numeric values when you do iTotal = iTotal + iNum
If you *still* have problems, you can explicitly do the conversion:
iTotal = iTotal + Val(iNum)
HTH,
I'd rather die peacefully in my sleep like my Grandfather,
than screaming in terror like his passengers. Jim Harkins
<a href="http://www.Horrible.Demon.co.uk/" target="_blank">http://www.Horrible.Demon.co.uk/</a>
-
October 24th, 2001, 10:44 AM
#9
-
October 24th, 2001, 08:22 PM
#10
got it working..
<code>
Dim SplitArray As Variant
Dim iNum As Variant
Dim iTotal As Long
SplitArray = Split(Text2.Text, " ")
For Each iNum In SplitArray
'iTotal = iTotal + iNum
iTotal = iTotal + Val(iNum)
Next
Text3.Text = iTotal & "%"
</code>
theres the code
-
October 25th, 2001, 03:10 AM
#11
The reason that you were having problems is because VB is a poorly typed language...
This means that it does implicit conversions between different types, depending on what it thinks you mean.
Obviously sometimes this goes wrong and you get unpredictable results!
For example, the .value of a text box is always a String, but you can implicitly convert it into a numeric (long) value like this:
[code]
Dim iValue as Long
Dim sValue as String
sValue = txtBox.Value
iValue = iValue + sValue
</pre><hr></blockquote>
This is why it's important to get your variable declarations right.
However, you can also use the "+" operator on strings to concatenate them, so if you get your declarations wrong, it will concatenate the string together. This is syntactically correct, because adding strings together should concatenate them...
[code]
Dim iValue as String
Dim sValue as String
sValue = txtBox.Value
iValue = iValue + sValue
</pre><hr></blockquote>
You would now find that the string is concatenated even if it contains number, like you did above. But this may not be what you actually meant to do - VB just can't tell the difference!
This is why it's very bad to use the "+" operator as a string concatenator - always use the correct "&" operator instead.
The problem is that a "variant" data type means that it can be anything - string, long, boolean, date, etc - and VB has to make the "best guess" depending on what you are doing.
This is why it's also bad to use variants in your programs and where you must have to use variants (ie, in our example above to use FOR EACH ... ) then you should ensure that your declarations are correct or that you explicitly convert your data types to remove any amibguity.
You don't get this problem in strongly typed languages, such as C/C++, because it won't do implicit conversions and your program will error.
HTH,
I'd rather die peacefully in my sleep like my Grandfather,
than screaming in terror like his passengers. Jim Harkins
<a href="http://www.Horrible.Demon.co.uk/" target="_blank">http://www.Horrible.Demon.co.uk/</a>
-
October 25th, 2001, 08:35 AM
#12
I see, Yeah I was just playing around with a program I thought to make, but it was one of those programs when your were bored. But im trying to get into GTK more for my MacOSX/Linux/FreeBSD Box's. So here ill learn C ehhe I hope.
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
|
|
Bookmarks