Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 21st, 2007, 11:17 PM   #1
garyelmer
Newbie
 
Join Date: Oct 2007
Posts: 8
Rep Power: 0 garyelmer is on a distinguished road
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.
garyelmer is offline   Reply With Quote
Old Oct 21st, 2007, 11:35 PM   #2
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 4 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
Re: Adding contents of a list box

You are not totaling decTotalCost you simply reasign the last value of lstCost.ITems(intCount)
__________________
JG-Webdesign
Wizard1988 is offline   Reply With Quote
Old Oct 21st, 2007, 11:43 PM   #3
garyelmer
Newbie
 
Join Date: Oct 2007
Posts: 8
Rep Power: 0 garyelmer is on a distinguished road
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.
garyelmer is offline   Reply With Quote
Old Oct 21st, 2007, 11:47 PM   #4
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 4 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
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
Wizard1988 is offline   Reply With Quote
Old Oct 22nd, 2007, 12:07 AM   #5
garyelmer
Newbie
 
Join Date: Oct 2007
Posts: 8
Rep Power: 0 garyelmer is on a distinguished road
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.
garyelmer is offline   Reply With Quote
Old Oct 22nd, 2007, 12:43 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Oct 22nd, 2007, 12:43 AM   #7
garyelmer
Newbie
 
Join Date: Oct 2007
Posts: 8
Rep Power: 0 garyelmer is on a distinguished road
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.
garyelmer is offline   Reply With Quote
Old Oct 22nd, 2007, 12:49 AM   #8
garyelmer
Newbie
 
Join Date: Oct 2007
Posts: 8
Rep Power: 0 garyelmer is on a distinguished road
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
garyelmer is offline   Reply With Quote
Old Oct 22nd, 2007, 5:02 AM   #9
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,035
Rep Power: 5 lectricpharaoh will become famous soon enough
Re: Adding contents of a list box

Quote:
Originally Posted by garyelmer
What am I missing here?
Code tags.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is online now   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:47 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC