|
-
Oct 14th, 2001, 12:06 PM
#1
Thread Starter
Fanatic Member
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:
Option Explicit
Public thm As Integer
Private Sub Form_Load()
thm = thm + 1
End Sub
'To add each thumbnails (works fine)
Private Sub MakeThmnails_Click()
Static lCounter As Long
Load Image2(thm) ' Create new button.
Image2(thm).Top = 0
Image2(thm).Left = Image2(thm - 1).Left + 50 + Image2(thm - 1).Width
'Set new control to the right of previous
Image2(thm).Visible = True
Image2(thm).Stretch = True
Image2(thm).BorderStyle = 1
Image2(thm).ToolTipText = Combo1.Text
Picture1.Width = Picture1.Width + 1092
Image2(thm).Picture = LoadPicture(File2.Path & "\" & File2.List(lCounter))
thm = thm + 1
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:
'Remove code (needs to be modified)
Private Sub Image2_MouseDown(Index As Integer, Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = 2 Then
For thm = Index To thm - 1
Unload Image2(thm)
Next
thm = Index
End If
End Sub
-
Oct 14th, 2001, 12:18 PM
#2
PowerPoster
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:
Dim MyArray() As String
Dim NewArray() As String
Dim i As Long
ReDim MyArray(4) As String
'populate it for this example
For i = 0 To UBound(MyArray)
MyArray(i) = "Hello " & i
Next i
'we want to remove the 3rd element (index=2)
Dim intCount As Long
intCount = 0
ReDim NewArray(UBound(MyArray) - 1)
For i = 0 To UBound(MyArray)
If i <> 2 Then
NewArray(intCount) = MyArray(i)
intCount = intCount + 1
End If
Next i
-
Oct 14th, 2001, 12:56 PM
#3
Thread Starter
Fanatic Member
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:
Private Sub Image2_MouseDown(Index As Integer, Button As Integer, Shift As Integer, x As Single, y As Single)
Dim Image2() As String
Dim NewArray() As String
Dim intCount As Long
If Button = 2 Then
intCount = 0
ReDim NewArray(UBound(Image2) - 1)
For thm = 0 To UBound(Image2)
If thm <> Index Then
NewArray(intCount) = Image2(thm)
intCount = intCount + 1
End If
Next thm
End If
End Sub
-
Oct 14th, 2001, 01:05 PM
#4
PowerPoster
which line does it error on? Usually that means you've tried to access an element that doesn't exist
-
Oct 14th, 2001, 01:17 PM
#5
Thread Starter
Fanatic Member
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
-
Oct 14th, 2001, 01:20 PM
#6
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|