Removing items from listbox
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
Re: Removing items from listbox
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
Re: Removing items from listbox
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
[email protected]
Re: Removing items from listbox
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