Results 1 to 5 of 5

Thread: delete in collection and listbox(RESOLVED)

  1. #1

    Thread Starter
    Lively Member SunFlower_Queen's Avatar
    Join Date
    Nov 2004
    Posts
    101

    Resolved 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:
    1. Public Sub DeleteControl(ByVal Index As Integer)
    2.     'delete item from collection
    3.     Me.FormControls.Remove CStr(Index)
    4.            
    5. '       change key for all collection items that are after deleted item
    6. '       remove item with key(index) and add it as key(index - 1)
    7.     For Index = Index To FormControls.Count - 1
    8.         Me.FormControls.Add FormControls(CStr(Index + 1)), CStr(Index)
    9.         Me.FormControls.Remove CStr(Index + 1)
    10.        
    11.     Next
    12. 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

  2. #2
    Fanatic Member ALL's Avatar
    Join Date
    Jul 2004
    Location
    192.168.1.1
    Posts
    711

    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
    Please support one of my projects?
    TKForums.com

    Web Forum
    JavaScript Wiki
    ________________________
    If somone helps you, please rate their post, by clicking the to rate their post

  3. #3

    Thread Starter
    Lively Member SunFlower_Queen's Avatar
    Join Date
    Nov 2004
    Posts
    101
    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

  4. #4
    Fanatic Member ALL's Avatar
    Join Date
    Jul 2004
    Location
    192.168.1.1
    Posts
    711
    once i get to my VB work i will try to fix the problem

    i will try to get it solved today
    Please support one of my projects?
    TKForums.com

    Web Forum
    JavaScript Wiki
    ________________________
    If somone helps you, please rate their post, by clicking the to rate their post

  5. #5

    Thread Starter
    Lively Member SunFlower_Queen's Avatar
    Join Date
    Nov 2004
    Posts
    101
    Thanks but I figured it out. My new code looks like this and it works.(thanks tec-nico)
    VB Code:
    1. Dim TempObject As CDWControl
    2.    
    3. '    Me.FormControls.Remove CStr(Index)
    4.        
    5.      With FormControls
    6.         'delete item from collection
    7.      .Remove CStr(Index)
    8.         'loop through collection
    9.         For Index = Index To .Count
    10.            Set TempObject = .Item(CStr(Index + 1))
    11.            .Remove CStr(Index + 1)
    12.            .Add TempObject, CStr(Index)
    13.         Next
    14.      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
  •  



Click Here to Expand Forum to Full Width