Easily, how can I add up numeric values in a listbox and display the total in a text field.
i.e lets say this is my list box
------------------
0.75
0.95
1.25
3
1.55
------------------
how can the total or sum be found.
Printable View
Easily, how can I add up numeric values in a listbox and display the total in a text field.
i.e lets say this is my list box
------------------
0.75
0.95
1.25
3
1.55
------------------
how can the total or sum be found.
I didn't have time to test that so I hope it works ;)Code:dim counter as integer
dim tempAnswer as integer
tempAnswer = 0
for counter = 0 to 4
List1.listindex = counter
TempAnswer = tempanswer + val(list1.text)
next counter
text1.text = tempanswer
I didn't test it but it should be singles instead of integersCode:dim counter as single
dim tempAnswer as single
tempAnswer = 0
for counter = 0 to 4
List1.listindex = counter
TempAnswer = tempanswer + val(list1.text)
next counter
text1.text = tempanswer
woops, didn't notice that :) thanks dimava
why is the "counter = 0 to 4", why is it 0 to 4??
It is 0 to 4 because you have 5 values in your listbox and the listindex starts with 0, therefore you have 0 to 4. To account for all 5 values.
yes but what if the number on items in the listbox are undetermined and are added to everytime.
the reason i say is because i am working or a program used to keep a total of daily sales made in a store. so everytime a receipt is calculated the total is added to the this listbox and this happens everytime a new receipt is calculated, in turn i need to calculate a daily total, for that i must find a way of adding all the values in the listbox and displaying a daily total in the text field.
thanks
In that case you would use 0 to list1.ListCount instead of some number. The ListCount property gives you the number of values in your Listbox. So you would say counter = 0 to List1.ListCount
You will use
counter = 0 to List1.ListCount-1
listcount starts at 1 while the list starts at 0.