VB6 Programming Question

G

Gary Bouchard

Hello all,

I have a group of object which have an element called ctlindex.

What I would like to do is randomly change the control index (ctlindex)
within a range of number to shuffle them....

I have 12 image controls in an array, and I may have anywhere from 1 to 12
images to populate those controls, but typically have maybe 6 or 12 images.

imagectl.index = 12 to 23

What I want to do is shuffle the images, but only the occupied image controls.

In other words; the first objects ctlindex = 12 and the last one is 23, but
there may not be one in each slot... I might have only 6 objects, and want
to keep them contiguous, but shuffle the images.

Can someone give me a direction to go with this?

I can get the images to shuffle, but I end up with an empty image control in
between occupied ones and I want to keep them all together.

Any assistance is appreciated.
 
G

GeoffG

You can copy and paste the following code.
I think this will achieve your objective.


Option Explicit

Private malngOriginalTop(0 To 11) As Long
Private malngOriginalLeft(0 To 11) As Long


Private Sub Form_Load()

' Form load event procedure.

Dim I As Integer

' Store original image positions:
For I = 0 To 11
malngOriginalTop(I) = Image1(I).Top
malngOriginalLeft(I) = Image1(I).Left
Next

' Initialize random-number generator:
Randomize

End Sub

Private Sub cmdArrange_Click()

' This is the click event of
' the command button named cmdArrange.
'
' Randomly select images containing pictures
' and move them into consecutive positions.

Dim I As Integer
Dim J As Integer
Dim aintRandom(0 To 11) As Integer
Dim intRnd1 As Integer
Dim intRnd2 As Integer
Dim intTemp As Integer

' Restore original image positions
' and visibility:
Call cmdRestore_Click

' Fill array with 12 ascending
' different integers:
For I = 0 To 11
aintRandom(I) = I
Next

' Randomly swap array elements:
For I = 1 To 1000
intRnd1 = Int(12 * Rnd)
intRnd2 = Int(12 * Rnd)
intTemp = aintRandom(intRnd1)
aintRandom(intRnd1) = aintRandom(intRnd2)
aintRandom(intRnd2) = intTemp
Next

' Randomly select images containing
' pictures and move them into
' consecutive positions:
J = 0
For I = 0 To 11
With Image1(aintRandom(I))
If .Picture.Type > 0 Then
.Visible = True
.Top = malngOriginalTop(J)
.Left = malngOriginalLeft(J)
J = J + 1
Else
.Visible = False
End If
End With
Next

End Sub

Private Sub cmdRestore_Click()

' This is the click event of
' the command button named cmdRestore.
'
' Restore images to their original positions.

Dim I As Integer

' Restore original image positions
' and visibility:
For I = 0 To 11
Image1(I).Top = malngOriginalTop(I)
Image1(I).Left = malngOriginalLeft(I)
Image1(I).Visible = True
Next

End Sub


For future reference, I think this newsgroup is supposed to
be dedicated to discussions about programming DAO with VBA
in Microsoft Access. You're more likely to get a better
answer to your type of question in a newsgroup dedicated to
VB (especially as Access doesn't support control arrays).
For example, this newsgroup might have been better:

microsoft.public.vb.discussion

Geoff




"Gary Bouchard" <[email protected]>
wrote in message
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top