# VB ListBox Ckecked Items



## Russell Morton (Apr 2, 1999)

From vb5 up you have been able to set the style of the listbox to include a check box at each item. I can find an event thats fired when the checkbox is changed, but how do I get the "value" of a list item's checkbox ?


----------



## gaorr (Oct 22, 2002)

I've only used VB6, but I am assuming it is the same.

choose style as 1 - checkbox

Using the following subroutine, Item will contain the value of selection in the listbox when it is checked. Remember the first Item in the list will have a value of zero.

Private Sub List1_ItemCheck(Item As Integer)

End Sub 

Hope this helps.


----------



## Russell Morton (Apr 2, 1999)

Am I correct in saying that if I use 

list1_checkItem(Index)

Index is teturned as either vbChecked or vbUnchecked for the currently selected item.

Thus if I want to make an array of all the checked items in a list I need to go through the list and select each item then call checkItem.


----------



## Mosaic1 (Aug 17, 2001)

Here's some code I used in a cookie utility I made. The Form has two Listboxes. One was named lstDelete and the other lstProtect. Any checked items in lstDelete were added to lstProtect (and then removed from lstDelete) The User has the option of choosing multiple items or only one item and then pressing the command button to move the checked items to the other listbox.



Private Sub cmdAddtoProtected_Click()
Dim i As Integer
If lstDelete.ListIndex = -1 Then
MsgBox "There are no cookies to protect"
Exit Sub
End If

For i = lstDelete.ListCount - 1 To 0 Step -1
If lstDelete.Selected(i) = True Then
ListProtect.AddItem lstDelete.List(i)
lstDelete.RemoveItem i
DoEvents
End If
Next i
End Sub


----------



## Russell Morton (Apr 2, 1999)

I will give this a try...


----------



## Russell Morton (Apr 2, 1999)

That worked fine, I would have thought that the selected property was for listboxes where multiple selection was enabled.

Many thanks for your help


----------

