Click to See Complete Forum and Search --> : Removing items from listbox


Simonkale
September 14th, 2001, 08:14 AM
The following codes return error "invalid property array index"

If OpenRegistry = true then
Dim strKeyValue as string
Dim J as Integer
for J = 0 to frmRega.lstRegEntries.ListCount - 1
If frmRega.lstRegEntries.Selected(J) = true then
strKeyValue = Trim$(Str(J))
lngReturnValues = RegDeleteValue(hKey, byval strKeyValue)
If lngReturnValues = ERROR_SUCCESS then
frmRega.lstRegEntries.RemoveItem (J)
MsgBox "success!"
End If
End If
next J
End If



The item is deleted but error occurs when the integer J is equal to
the ListCount.

Thanks,
Simon

d.paulson
September 14th, 2001, 08:37 AM
Try looping through the list in reverse. Change this line

for J = 0 to frmRega.lstRegEntries.ListCount - 1
to
for J = frmRega.lstRegEntries.ListCount - 1 to 0 step -1

The problem with your code is that when an item is removed, the listcount that you had before is smaller by 1, and you try to access an item of the list that no longer exists.


David Paulson

enigmaos
September 14th, 2001, 08:37 AM
try EXIT FOR - that's just for removing 1 item at a time. is that what you wanted it to do? If so, that will exit the for loop after it's been removed from the list.


Dim strKeyValue as string
Dim J as Integer

for J = 0 to frmRega.lstRegEntries.ListCount -1
If frmRega.lstRegEntries.Selected(J) then
strKeyValue = Trim$(Str(J))
lngReturnValues = RegDeleteValue(hKey, byval strKeyValue)
If lngReturnValues = ERROR_SUCCESS then
frmRega.lstRegEntries.RemoveItem (J)
MsgBox "success!"
'### Add this line ###
Exit for
'#####################
End If
End If
next J




enigmaos@yahoo.com

Simonkale
September 14th, 2001, 10:39 AM
Thanks d.paulson! That was exactly the problem with the code.
enigmaos, I had thought about using Exit For but the problem was the I couldn't limit the user to delete an entry at a time. Infact they can delete all entries at a time if they want.

Thanks for the help!
Simon