Results 1 to 6 of 6

Thread: Removing an array item. How hard?

  1. #1

    Thread Starter
    Fanatic Member daydee's Avatar
    Join Date
    Jun 2001
    Location
    Canada
    Posts
    560

    Removing an array item. How hard?

    Hi all, working with a bunch of image control in an array. How would I go about removing one of the image control (array item) and replace it with the one next to it, and then reduce the array size to "array size - 1"?

    In my case, user creates bunch of thumnails and later by right clicking on one of them, the thumbnail would automatically be deleted from the array and rest of the existing thumbnails on the right of it would be relocated to fill up the gap.

    VB Code:
    1. Option Explicit
    2. Public thm As Integer
    3.  
    4. Private Sub Form_Load()
    5.    thm = thm + 1
    6. End Sub
    7.  
    8. 'To add each thumbnails (works fine)
    9. Private Sub MakeThmnails_Click()
    10. Static lCounter As Long
    11.           Load Image2(thm)  ' Create new button.
    12.           Image2(thm).Top = 0  
    13.           Image2(thm).Left = Image2(thm - 1).Left + 50 + Image2(thm - 1).Width
    14.           'Set new control to the right of previous
    15.           Image2(thm).Visible = True
    16.           Image2(thm).Stretch = True
    17.           Image2(thm).BorderStyle = 1
    18.           Image2(thm).ToolTipText = Combo1.Text
    19.           Picture1.Width = Picture1.Width + 1092
    20.           Image2(thm).Picture = LoadPicture(File2.Path & "\" & File2.List(lCounter))
    21.        thm = thm + 1
    22. End Sub

    With code below I can remove all the thumnails to the right of the one user right clicks on but that's about as far as I've succeeded.
    Thanks in advance.
    VB Code:
    1. 'Remove code (needs to be modified)
    2. Private Sub Image2_MouseDown(Index As Integer, Button As Integer, Shift As Integer, x As Single, y As Single)
    3. If Button = 2 Then
    4.   For thm = Index To thm - 1
    5.        Unload Image2(thm)
    6.        Next
    7.        thm = Index
    8.   End If
    9. End Sub

  2. #2
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    The easiest way to remove elements from the middle of an array is to create a new one, and only copying over the elements you want from the old to the new, a bit like this (haven't tested it!)
    VB Code:
    1. Dim MyArray() As String
    2. Dim NewArray() As String
    3. Dim i As Long
    4. ReDim MyArray(4) As String
    5.  
    6. 'populate it for this example
    7. For i = 0 To UBound(MyArray)
    8.     MyArray(i) = "Hello " & i
    9. Next i
    10.  
    11. 'we want to remove the 3rd element (index=2)
    12. Dim intCount As Long
    13. intCount = 0
    14.  
    15. ReDim NewArray(UBound(MyArray) - 1)
    16.  
    17. For i = 0 To UBound(MyArray)
    18.     If i <> 2 Then
    19.         NewArray(intCount) = MyArray(i)
    20.         intCount = intCount + 1
    21.     End If
    22. Next i

  3. #3

    Thread Starter
    Fanatic Member daydee's Avatar
    Join Date
    Jun 2001
    Location
    Canada
    Posts
    560

    getting there!

    chrisjk, I understand partly but any chance you could show me how to work it in my code?

    I've tried this below but I get a Subscrit out of range error!
    VB Code:
    1. Private Sub Image2_MouseDown(Index As Integer, Button As Integer, Shift As Integer, x As Single, y As Single)
    2. Dim Image2() As String
    3. Dim NewArray() As String
    4. Dim intCount As Long
    5.  
    6. If Button = 2 Then
    7. intCount = 0
    8. ReDim NewArray(UBound(Image2) - 1)
    9.  
    10. For thm = 0 To UBound(Image2)
    11.     If thm <> Index Then
    12.         NewArray(intCount) = Image2(thm)
    13.         intCount = intCount + 1
    14.     End If
    15. Next thm
    16. End If
    17. End Sub

  4. #4
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    which line does it error on? Usually that means you've tried to access an element that doesn't exist

  5. #5

    Thread Starter
    Fanatic Member daydee's Avatar
    Join Date
    Jun 2001
    Location
    Canada
    Posts
    560

    Re"error

    The error happens on this line
    ReDim NewArray(UBound(Image2) - 1)
    but also I was thinkin, if renaming it is the only way to go, wont I loose all my click events (I need those same click event for the newly created array)
    Hope you understand what I mean.
    Thanks

  6. #6

    Thread Starter
    Fanatic Member daydee's Avatar
    Join Date
    Jun 2001
    Location
    Canada
    Posts
    560

    oops!

    I meant create a new one not "rename" , sorry

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