Question Help with Looping and Listbox data

krcostello87

New member
Joined
Apr 19, 2010
Messages
1
Programming Experience
1-3
Hey guys gotta quick question for ya. I'm creating a program called Workshop Selector. It has 3 list boxes, one with the 5 workshops, second with 6 locations, and the final is the list of costs. The user selects a workshop, a location, then hits the Add Workshop button, which calculates the costs and adds the cost to the third listbox. Soo, to my question.

There is an additional button called Calculate Total that adds the values of the costs in the cost listbox and displays the total in a textbox. The code i currently have that works just fine is --

Dim one, two, three, four, five, total As Integer
lbCost.SelectedIndex = 0
one = CInt(lbCost.SelectedItem.ToString)
lbCost.SelectedIndex = 1
two = CInt(lbCost.SelectedItem.ToString)
etc..etc..
total = one + two + three + four + five
tbxTotal.text = total

While this works, i have no idea of knowing how many times the user will add a cost to the cost listbox. therefore id have to make many many variables, and other issues occur.
So is there a way i can do this with a for loop? declare a global variable like

Dim costcounter as integer

then at the end of the add workshop cost btn click procedure use:
costcounter = costcounter + 1

That way i kno the exact amount of times the person added a cost?

Then create a for loop like

Dim intcount As Integer
For intcount = 1 To costcounter
code....
Next

Would this work somehow? but what code would i have to input within the for loop to scan each selected index, and then return values that can be added together and displayed in a textbox?
 

Attachments

  • WorkshopSelector.zip
    15.8 KB · Views: 90
Last edited by a moderator:
You don't need to keep track of the number of items in the ListBox because it does so itself. ListBox.Items.Count will give you the number of items. The Items property is a collection, so it behaves like other collections. That means that you can enumerate it using a For Each loop, which allows you to visit each and every item in the collection. In your case, you can add those items to a running total as you go. I suggest that you go back and review For Each loops.
 
Back
Top