By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,786 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,786 IT Pros & Developers. It's quick & easy.

Listbox SelectedItem addition

P: 2
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
May 14 '07 #1
Share this Question
Share on Google+
4 Replies


P: 2
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?
May 14 '07 #2

danp129
Expert 100+
P: 321
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
May 14 '07 #3

Expert 5K+
P: 8,434
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.
May 15 '07 #4

Expert 5K+
P: 8,434
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
May 21 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.