![]() |
Adding contents of a list box
I'm enrolled in VB.net at a local community college. We're using Visual Basic 2005 Text Book.
My assignment is to create a form that does several things which was easy until I came to this part . . . Add the contents of the ListBox and show the total in a label. If there were only 2 items it would be easy, but there is a varying number of items. Chapter 5 also deals with loops so I'm thinking of using a For . . .Next loop. I can get the number of items in the list box into a label but I am REALLY stuck on totalling all the numbers in the list box. Example: In lstTotals there are 5 items in lstCosts 250 525 375 600 125 and here is what I have so far, which isn't working: intListSize = lstCosts.Items.Count - 1 For intCount = 0 To intListSize decTotalCosts = lstCosts.Items(intCount) intCount += 1 Next intCount lblTotalCost.Text = decTotalCosts What am I missing here? Thank you in advance. |
Re: Adding contents of a list box
You are not totaling decTotalCost you simply reasign the last value of lstCost.ITems(intCount)
|
Re: Adding contents of a list box
I know, it isn't working.
My question is how do you total a bunch of values in a listbox using a for . . next loop. |
Re: Adding contents of a list box
change:
decTotalCosts = lstCosts.Items(intCount) to: decTotalCosts += lstCosts.Items(intCount) if you don't know what the += operator does you can use this decTotalCost = decTotalCost + lstCost.Items(intCount) |
Re: Adding contents of a list box
Okay, I did that and here is what I have so far . . .
intListSize = lstCosts.Items.Count - 1 For intCount = 0 To intListSize decTotalCosts += lstCosts.Items(intCount) intCount += 1 Next intCount lblTotalCost.Text = decTotalCosts What it is doing now is adding item 0, 2, 4 and so on. It is skipping 1, 3, 5 etc. |
Re: Adding contents of a list box
That's because you're incrementing intCount twice per loop. Read about what "Next" does....
|
Re: Adding contents of a list box
Oh well,
I've been working on this one for way too long. Maybe the answer will come to me in a dream. Thank you for your help. Good night. |
Re: Adding contents of a list box
Hey Mr. Wei,
Thank you for that post. Based on what you said I simply deleted: intCount = intCount + 1 and all is good. I am a total newbie at this stuff and I don't really understand why it's working it just is. I will study it more tomorrow. Good Night! Zzzzzzzzzzzzzzzzzzzzzz |
Re: Adding contents of a list box
Quote:
|
| All times are GMT -5. The time now is 3:35 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC