|
-
Dec 2nd, 2004, 09:24 AM
#1
Thread Starter
Lively Member
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
Last edited by SunFlower_Queen; Dec 2nd, 2004 at 03:10 PM.
~*~*~*~*~*~*
Sunflower Queen
If you have knowledge, let others light their candles at it.
-- Margaret Fuller
-
Dec 2nd, 2004, 10:01 AM
#2
Fanatic Member
Re: delete in collection and listbox
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
-
Dec 2nd, 2004, 10:29 AM
#3
Thread Starter
Lively Member
Thanks ALL,
but I couldn't really understand how to make it work in my program. What I want to do is take the object after the one I deleted and change the key to the key of the deleted one. I used some code to delete an item from an array this way and it is the same idea I just need to figure out how to take collectionItem(ID:2) and make it collectionItem(ID:1) then make 3 into 2 and so on down the line.
Not sure maybe the link you gave me would work I just can't figure it out.
~*~*~*~*~*~*
Sunflower Queen
If you have knowledge, let others light their candles at it.
-- Margaret Fuller
-
Dec 2nd, 2004, 02:15 PM
#4
Fanatic Member
once i get to my VB work i will try to fix the problem
i will try to get it solved today
-
Dec 2nd, 2004, 02:38 PM
#5
Thread Starter
Lively Member
Thanks but I figured it out. My new code looks like this and it works.(thanks tec-nico)
VB Code:
Dim TempObject As CDWControl
' Me.FormControls.Remove CStr(Index)
With FormControls
'delete item from collection
.Remove CStr(Index)
'loop through collection
For Index = Index To .Count
Set TempObject = .Item(CStr(Index + 1))
.Remove CStr(Index + 1)
.Add TempObject, CStr(Index)
Next
End With
~*~*~*~*~*~*
Sunflower Queen
If you have knowledge, let others light their candles at it.
-- Margaret Fuller
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|