Connecting Tech Pros Worldwide Forums | Help | Site Map

Listbox SelectedItem addition

Newbie
 
Join Date: May 2007
Posts: 2
#1: May 14 '07
I need some help with this, just can't seem to get it straight.


I need to write a loop that will add each item of a listbox to get a Sum of the numbers inside.

I am using vb.net 2005

Newbie
 
Join Date: May 2007
Posts: 2
#2: May 14 '07

re: Listbox SelectedItem addition


Here is what i have so far...

Expand|Select|Wrap|Line Numbers
  1.  intTnum = lstNum.Items.Count
  2.  
  3.         For intX = 0 To intTnum
  4.             intTLoop = lstNum.Items(0)
  5.             intTSum = intTSum + intTLoop
  6.             intX += 1
  7.         Next intX
How can i increment lstNum.Items(0) to the next number with each repetition of the loop?
danp129's Avatar
Expert
 
Join Date: Jul 2006
Posts: 250
#3: May 15 '07

re: Listbox SelectedItem addition


Didn't test but this should work:
Expand|Select|Wrap|Line Numbers
  1. For intX = 0 To lstNum.Items.Count
  2.     intTLoop = lstNum.Items(intX)
  3.     intTSum = intTSum + intTLoop
  4. Next 'intX this is optional
Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#4: May 15 '07

re: Listbox SelectedItem addition


Quote:

Originally Posted by manch

Here is what i have so far...

Expand|Select|Wrap|Line Numbers
  1.  intTnum = lstNum.Items.Count
  2.  
  3.         For intX = 0 To intTnum
  4.             intTLoop = lstNum.Items(0)
  5.             intTSum = intTSum + intTLoop
  6.             intX += 1
  7.         Next intX
...

You're actually very close. Just two problems jump out at me...
  1. lstNum.Items(0) should be lstNum.Items(intX)
  2. The For loop automatically increments the counter (intX in this case). So by adding 1 to it again, you will be skipping every second item.
Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#5: May 22 '07

re: Listbox SelectedItem addition


Quote:

Originally Posted by ciscohomie

I have a question about list box too if i may..

I'm going to split this question and my response to a new thread, just so they're easier for others to find. I'll come back and put a link here.

...

Ok, it's How to sum a 'column' in a listbox holding tab-delimited text
Reply


Similar Visual Basic 4 / 5 / 6 bytes