![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2007
Posts: 8
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Professional Programmer
|
Re: Adding contents of a list box
You are not totaling decTotalCost you simply reasign the last value of lstCost.ITems(intCount)
__________________
JG-Webdesign |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2007
Posts: 8
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#4 |
|
Professional Programmer
|
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)
__________________
JG-Webdesign |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Oct 2007
Posts: 8
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Re: Adding contents of a list box
That's because you're incrementing intCount twice per loop. Read about what "Next" does....
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Oct 2007
Posts: 8
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Oct 2007
Posts: 8
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#9 | |
|
Caffeinated Neural Net
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 925
Rep Power: 4
![]() |
Re: Adding contents of a list box
Quote:
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| linked list problems | bl00dninja | C++ | 6 | Feb 17th, 2008 10:30 AM |
| singly-linked list templaste class in C++ w/ example driver | bl00dninja | Show Off Your Open Source Projects | 0 | Sep 11th, 2006 1:05 AM |
| Singly Linked List Help | Firebar | Java | 3 | May 22nd, 2005 10:56 AM |
| User-defined creatNode and deleteNode functions for a doubly-linked list | jgs | C | 2 | Apr 28th, 2005 8:53 AM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C++ | 0 | Mar 2nd, 2005 4:12 PM |