How to add the values of items in a list box in Visual Basic?
How do you add the numeric values within a list box to equal a sum for the list box? I know how to add numbers to a list box, but I am unsure on how to add the numeric values to have a total value for the box. Any help is appreciated.
Public Comments
- Dim counter As Integer = 0 For i As Integer = 0 To (ListBox1.Items.Count - 1) counter += Val(ListBox1.Items.Item(i)) Next Label1.Text = counter what this code snippet will do is add the value of numbers in the listbox from item 1 to the last item into the counter. then put that value in a label1 for you to see. the ListBox1.Items.Item(number), which is 0 based, that means the count starts at 0 instead of 1, it will return whatever is in that location in the listbox where number is an integer. the ListBox1.Items.Count returns the total number of items in your listbox.
Powered by Yahoo! Answers