Listbox SelectedItem addition

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manch
    New Member
    • May 2007
    • 2

    Listbox SelectedItem addition

    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
  • manch
    New Member
    • May 2007
    • 2

    #2
    Here is what i have so far...

    Code:
     intTnum = lstNum.Items.Count
    
            For intX = 0 To intTnum
                intTLoop = lstNum.Items(0)
                intTSum = intTSum + intTLoop
                intX += 1
            Next intX
    How can i increment lstNum.Items(0) to the next number with each repetition of the loop?

    Comment

    • danp129
      Recognized Expert Contributor
      • Jul 2006
      • 323

      #3
      Didn't test but this should work:
      Code:
      For intX = 0 To lstNum.Items.Count
          intTLoop = lstNum.Items(intX)
          intTSum = intTSum + intTLoop
      Next 'intX this is optional

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by manch
        Here is what i have so far...

        Code:
         intTnum = lstNum.Items.Count
        
                For intX = 0 To intTnum
                    intTLoop = lstNum.Items([B]0[/B])
                    intTSum = intTSum + intTLoop
                    [B]intX += 1[/B]
                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.

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          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

          Comment

          Working...