delete in collection and listbox(RESOLVED)
This piece of code is used in a class module to delete an item form a collection of objects in that class. It also has to re-key the remaining items to match the indexes in a listbox so it always deletes the same item from the list and collection. It works for the first delete but then consistently deletes the item after the selected one. Can anyone see what is wrong with this code?
VB Code:
Public Sub DeleteControl(ByVal Index As Integer)
'delete item from collection
Me.FormControls.Remove CStr(Index)
' change key for all collection items that are after deleted item
' remove item with key(index) and add it as key(index - 1)
For Index = Index To FormControls.Count - 1
Me.FormControls.Add FormControls(CStr(Index + 1)), CStr(Index)
Me.FormControls.Remove CStr(Index + 1)
Next
ENd sub
Re: delete in collection and listbox
Quote:
Originally posted by SunFlower_Queen
[B]This piece of code is used in a class module to delete an item form a collection of objects in that class. It also has to re-key the remaining items to match the indexes in a listbox so it always deletes the same item from the list and collection. It works for the first delete but then consistently deletes the item after the selected one. Can anyone see what is wrong with this code?
[Highlight=VB]
Public Sub DeleteControl(ByVal Index As Integer)
'delete item from collection
Me.FormControls.Remove CStr(Index)
' change key for all collection items that are after deleted item
' remove item with key(index) and add it as key(index - 1)
For Index = Index To FormControls.Count - 1
Me.FormControls.Add FormControls(CStr(Index + 1)), CStr(Index)
Me.FormControls.Remove CStr(Index + 1)
Next
ENd sub
i am not sure, but when you add a control you cannot do it like that, here is a link on how to add a command button, you can change the code a little to fit what you want:
http://vbforums.com/showthread.php?s...during+runtime
i hope that helps:thumb: