I have set up a dynamic shape array to add new shapes to the form during runtime, but am having trouble looping them.
This way works:
Option Explicit
Private WithEvents Block As Shape
Private Sub cmdCreate_Click()
Dim Block() As Shape
Dim Name As String
ReDim Block(5) As Shape
Dim X As Integer
Dim number As Integer
Name = "Block" & number
Set Block(0) = Me.Controls.Add("VB.Shape", Name, Me)
With Block(0)
.Visible = True
.Shape = 0
.BackStyle = 1
.BackColor = vbBlue
.Height = 250
.Width = 500
End With
number = number + 1
Name = "Block" & number
Set Block(1) = Me.Controls.Add("VB.Shape", Name, Me)
With Block(1)
.Visible = True
.Shape = 0
.BackStyle = 1
.BackColor = vbBlue
.Top = 500
.Height = 250
.Width = 500
End With
number = number + 1
Name = "Block" & number
Set Block(2) = Me.Controls.Add("VB.Shape", Name, Me)
With Block(2)
.Visible = True
.Shape = 0
.BackStyle = 1
.BackColor = vbBlue
.Top = 1000
.Height = 250
.Width = 500
End With
End Sub
When I add the loop, I get a fatal execution.
Option Explicit
Private WithEvents Block As Shape
Private Sub cmdCreate_Click()
Dim Block() As Shape
Dim Name As String
ReDim Block(5) As Shape
Dim X As Integer
For X = 0 To 5
Name = "Block" & X
Set Block(X) = Me.Controls.Add("VB.Shape", Name, Me)
With Block(X)
.Visible = True
.Shape = 0
.BackStyle = 1
.BackColor = vbBlue
.Top = .Top + (500 * X)
.Height = 250
.Width = 500
End With
Next X
End Sub
Can anyone see where the error is?
Thanks for the help.
