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
Printable View
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
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.
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.
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
[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.
[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 :D
HTH,
[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 :D
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
[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,
ahhh
I was Dim'ing them but I missed one :)
hope it works when I get home
Dim iArray as long
Dim iNum as variant
Dim iTotal as long
^^^ I did this (well on of the ways)
Dim iArray as variant
Dim iNum as variant
Dim iTotal as variant
and tryed
Dim iArray
Dim iNum as variant
Dim iTotal
But I guess thats my problem Ill try it :)
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 :)
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 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.