Results 1 to 8 of 8

Thread: Please how to know the number of items in a listview column?

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2021
    Posts
    45

    Please how to know the number of items in a listview column?

    Hello vbforums
    Please can anyone help me how to tell vb6:
    if there is more than one item in column1 do this.
    if there only one item in column2 do this
    if there no item in column3 do this
    thank you all
    Name:  pic5.png
Views: 85
Size:  1.5 KB

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,839

    Re: Please how to know the number of items in a listview column?

    You must count them.

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2021
    Posts
    45

    Re: Please how to know the number of items in a listview column?

    Quote Originally Posted by dilettante View Post
    You must count them.
    Would you please tell me how to do that.
    For the 1st column I can use
    Code:
    MsgBox Listview1.Listitems.Count
    What about the second and the third columns?
    thank you
    Last edited by Adebiyi24; Sep 28th, 2021 at 05:07 AM.

  4. #4
    Member
    Join Date
    Sep 2016
    Location
    Texas panhandle
    Posts
    58

    Re: Please how to know the number of items in a listview column?

    Listview1.Columnheaders.count

    Sorry, misread OP

    Mods: Delete my post

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,704

    Re: Please how to know the number of items in a listview column?

    You have to loop through the list and check to see if there is an entry in the subitem and keep track of how many that had an entry

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2021
    Posts
    45

    Re: Please how to know the number of items in a listview column?

    Quote Originally Posted by DataMiser View Post
    You have to loop through the list and check to see if there is an entry in the subitem and keep track of how many that had an entry
    thank you Mr DataMiser
    Would you please provide an example.
    please

  7. #7
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    4,702

    Re: Please how to know the number of items in a listview column?

    Listview1.Listitems.Count gives the number of the rows in the ListView
    ListView1.ColumnHeaders.Count gives the number of columns in the ListView

    This should give you something to work with.
    It's about filling a ListView, not counting the used subitems:
    Code:
    Private Sub Command1_Click()
      Dim oListItem As ListItem
      Dim lRow As Long, lCol As Long
      
      With ListView1
        .View = lvwReport
        .ListItems.Clear
        .ColumnHeaders.Clear
        .ColumnHeaders.Add , , "Column 1"
        .ColumnHeaders.Add , , "Column 2"
        .ColumnHeaders.Add , , "Column 3"
        For lRow = 1 To 9
          Set oListItem = .ListItems.Add(, , CStr(lRow))
          For lCol = 1 To .ColumnHeaders.Count - 1
            If Int(Rnd * 2) = 1 Then
              oListItem.SubItems(lCol) = "Hello"
            End If
          Next lCol
        Next lRow
      End With
        
      MsgBox "Number of Rows: " & CStr(ListView1.ListItems.Count) & vbLf & "Number of Columns: " & CStr(ListView1.ColumnHeaders.Count)
        
    End Sub

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,449

    Re: Please how to know the number of items in a listview column?

    you can try like this to count all columns of subitems into an array
    Code:
    Dim cnt() As Long, i As ListItem, s As Long
    With ListView1
        ReDim cnt(.ColumnHeaders.Count - 2)
        On Error Resume Next
        For Each i In .ListItems
            For s = 0 To i.ListSubItems.Count - 1
                If Not i.ListSubItems(s + 1) = "" Then
                cnt(s) = cnt(s) + 1
                End If
            Next
        Next
    End With
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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