Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

listview contents 1

Status
Not open for further replies.

kimtp

Programmer
Jun 15, 2002
310
US
Have filled a listview with checkboxes with ID, Lastname and firstname. I now want to add the ID to a collection but am having trouble identifying that all are in the collection. Here is what I have:

Code:
    Dim i As Integer
    Dim cCol As Collection
    Dim lstItem
    With listview1        
        For i = 1 To .ListItems.Count
            If .ListItems(i).Checked Then
                Set cCol = New Collection
                lngID = .ListItems(i).Checked
                ntCol.Add lngID
                MsgBox lngID
              End If
        Next i
    End With
.listitems(i).checked always returns -1. If I use .selecteditem, that item is always selected and returns only the ID for that item even if several checkboxes are checked.

Any help would be great. thanx.

Kim
 
Hi kimtp,

I see in your code that you declare cCol as a collection and set it to a new collection each time a .listitems(i).Checked is True. You may want to declare cCol as new collection in its dim statement and then add items to it in your loop. This way, cCol does not get initialized as a new collection each time you want to add something to it.

Also, you add the lngID to ntCol, not to cCol.

And as a third remark, lngID gets the value of the .Checked property, which is either True or False. It looks as though you want to assign a different value to the cCol collection, like a subitem or the dataitem of a selected item, rather than the value of the .Checked property. Where does the lngID value reside in your listview?

Hope this helps.


__________________
code is enduring
 
Thanks for the response. ntCol was a misprint. Changed the position of set ccol = new collection to before the loop.
 
The other thing that you could do is to use the ItemChecked event of the ListView and add/remove it from the collection when that event is fired.
 
Thankx bjd4jc. Have already incorporated the ItemCheck event.
 
Are you performing that code in the ItemCheck event?
 
Here is what I ended up with:

Code:
Private Sub cmdDoIt_Click()
    Set cCol = New Collection
    For Each xItem In lvwPrintNameTag.ListItems
        If xItem.Checked Then
            cCol.Add xItem.Text, xItem.Text
        End If
    Next
End Sub

and
Code:
Private Sub lvwprintnametag_ItemCheck(ByVal Item As MSComctlLib.ListItem)
  lvwPrintNameTag.ListItems(Item.Index).Selected = True
    For Each xItem In lvwPrintNameTag.ListItems
        If Not xItem.Checked Then
           'do my stuff here
        End If
    Next
End Sub

Guess I could combine them.
 
I created a new project, added a listview called lvwPrintNameTag and then added teh following code
Code:
Private cCol As Collection

Private Sub Form_Load()
    Set cCol = New Collection
    Dim LCV As Long
    
    For LCV = 0 To 10 Step 1
        lvwPrintNameTag.ListItems.Add , , LCV
    Next
    
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Set cCol = Nothing
End Sub

Private Sub lvwPrintNameTag_ItemCheck(ByVal Item As MSComctlLib.ListItem)
    If Item.Checked Then
        cCol.Add Item.Text, Item.Text
    Else
        Dim LCV As Long
        cCol.Remove Item.Text
    End If
    Debug.Print cCol.Count
End Sub

This seems to work fine - adding and removing the checked items from the collection as necessary.
 
Further I added a command button with this code to print out what was in the collection:

Code:
Private Sub Command1_Click()
    Dim LCV As Long
    
    For LCV = 1 To cCol.Count
        Debug.Print cCol(LCV)
    Next
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top