Results 1 to 7 of 7

Thread: [RESOLVED] Adding Up Numbers In ListView Column

  1. #1

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Resolved [RESOLVED] Adding Up Numbers In ListView Column

    I found this from a Google search and I need the VB.NET equivalent to Martin Liss' code
    Code:
    Dim lngIndex As Long
        Dim lngTot As Long
        
        For lngIndex = 1 To lv.ListItems.Count
            lngTot = lngTot + lv.ListItems(lngIndex).SubItems(1)
        Next
        
        MsgBox lngTot
    I have
    vb.net Code:
    1. Dim intRptTotal As Integer = 0
    2. For i As Integer = 1 To ListView1.Items.Count
    3.       intRptTotal = intRptTotal + CInt(ListView1.Items(i).SubItems(1))
    4. Next
    But, I'm getting the error
    Quote Originally Posted by Option Strict
    Value of type 'System.Windows.Forms.ListViewItem.ListViewSubItem' can not be converted to 'Integer'
    Basically, I need to total up a number column in a ListView.
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Adding Up Numbers In ListView Column

    You are getting a ListViewItem.ListViewSubItem here:
    vb.net Code:
    1. ListView1.Items(i).SubItems(1)
    Once you've got that object, you get the text it displays from its Text property.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: Adding Up Numbers In ListView Column

    I did add the Text property to it originally, but that gave me an error.
    Attached Images Attached Images  
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  4. #4
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Adding Up Numbers In ListView Column

    The Listview in .NET is 0-based (VB6 is 1-based), so if you have 53 items, then loop from 0 to 52. ListView1.Items.Count will return 53, which means you're looping from 0 to 53 and index 53 doesn't exist.

    You don't have to do "intRptTotal = intRptTotal + ....", but you can do "intRptTotal += ..." instead.

    vb.net Code:
    1. Dim intRptTotal As Integer = 0
    2. For i As Integer = 0 To lvwReportCount.Items.Count - 1
    3.       intRptTotal += CInt(lvwReportCount.Items(i).SubItems(1).Text)
    4. Next

    Alternatively, you can also do a for each loop.

    vb.net Code:
    1. For Each lvw As ListViewItem In lvwReportCount.Items
    2.     intRptTotal += CInt(lvw.SubItems(1).Text)
    3. Next
    Last edited by Chris001; Dec 29th, 2010 at 10:13 AM.

  5. #5
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Adding Up Numbers In ListView Column

    It's 0 based so you need to offset by 1 from the count.

    Code:
    For i As Integer = 0 To lvwReportCount.Items.Count - 1

  6. #6
    Addicted Member vb_ftw's Avatar
    Join Date
    Dec 2010
    Posts
    139

    Re: Adding Up Numbers In ListView Column

    what chris wrote...i added another line which will help u accomplish what u r after...

    vb Code:
    1. Dim intRptTotal As Integer = 0
    2.         For i As Integer = 0 To lvwReportCount.Items.Count - 1
    3.             intRptTotal += CInt(lvwReportCount.Items(i).Text)
    4.             intRptTotal += CInt(lvwReportCount.Items(i).SubItems(1).Text)
    5.         Next

    vb Code:
    1. For Each lvw As ListViewItem In lvwReportCount.Items
    2.             intRptTotal += CInt(lvw.Text)
    3.             intRptTotal += CInt(lvw.SubItems(1).Text)
    4.         Next

  7. #7

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: Adding Up Numbers In ListView Column

    Quote Originally Posted by Chris001 View Post
    The Listview in .NET is 0-based (VB6 is 1-based), so if you have 53 items, then loop from 0 to 52. ListView1.Items.Count will return 53, which means you're looping from 0 to 53 and index 53 doesn't exist.
    That is where I was going wrong...I was still using a 1-based loop. Thanks.
    Quote Originally Posted by Chris001 View Post
    You don't have to do "intRptTotal = intRptTotal + ....", but you can do "intRptTotal += ..." instead.
    I know I can do it that way, but I don't, and I won't.

    I find it far more clear to use variable = variable + newvariable

    Old dog...new tricks...etc.
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width