[RESOLVED] Control Arrays, Anyone?
I need to know how to make a control array in the Form_Load() sub routine. I am trying to make a platform game that is totally random in fact, that nothing is similar in each execution of the program.
So far I have this as my source code:
Code:
Dim i as Integer
Public Sub Form_Load()
For i = 0 to 99
End Sub
Re: Control Arrays, Anyone?
Try looking at this FAQ, post #3. Also, searching the forums for control arrays will return so many examples.
Re: Control Arrays, Anyone?
The easiest way would be to just add a control (example, a PictureBox) to the form and set its Index property to 0. Then you could have something like:
vb Code:
Option Explicit
Private Sub Form_Load()
Dim i As Integer
For i = 1 To 99
Load Picture1(i)
Picture1(i).Move i * Picture1(i).Width, 100
Picture1(i).Visible = True
Next i
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim i As Integer
For i = 1 To Picture1.UBound
Unload Picture1(i)
Next i
End Sub
Re: Control Arrays, Anyone?
How can I get the PictureBox control array to only move a random control in the array, into another position without disrupting all the rest.
eg:
Picture1(1).Top = "100"
Picture1(2).Top = "0"
Picture1(3).Top = "100"
And so on.
Also I don't wish this to only be one of them. But a randomly determined number of them, in fact.
Re: Control Arrays, Anyone?
Generate a random number to determine which control to move. Here is some code that doesn't quite do that, but should show you enough to get you there (I think). Start a project and put a picture box on the form. Call it "pb," and give it an index of 0 as mentioned earlier.
After the form loads, double click it to re-gen.
Code:
Option Explicit
Private Sub Form_DblClick()
Call randGen
End Sub
Private Sub randGen()
Dim myRand As Integer
Dim randLeft As Integer
Dim randTop As Integer
Dim i As Integer
Randomize
myRand = Int((99 - 1 + 1) * Rnd + 1) 'generates random number between 1 and 99
For i = 1 To myRand
randLeft = Int((Me.ScaleWidth - pb(0).Width - 1 + 1) * Rnd + 1)
randTop = Int((Me.ScaleHeight - pb(0).Height - 1 + 1) * Rnd + 1)
On Error Resume Next
Load pb(i)
pb(i).Left = randLeft
pb(i).Top = randTop
pb(i).Visible = True
Next i
Me.Caption = myRand
End Sub
Private Sub Form_Load()
Call randGen
End Sub