Results 1 to 4 of 4

Thread: Cant find error

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2002
    Location
    South Dakota
    Posts
    9

    Cant find error

    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.

  2. #2
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    Both work ok for me. Are u sure that u arent attempting to create a control that has been created earlier in code? What exact error are u getting?
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2002
    Location
    South Dakota
    Posts
    9
    no errors, just crashes my vb to desktop

  4. #4
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    I think u should go the control array method per your other threads.

    ie put one shape control on the form with Index = 0
    then
    VB Code:
    1. For x = 1 to 20
    2.    Load Shape1(x)
    3.    With Shape1(x)
    4.         .Top = 20
    5.         .Left = x * 10
    6.         .Visible = true
    7.    End With
    8. Next
    Also, it is quicker to loop thru setting the Visible property after all controls are loaded if there are a lot of controls... ie a seperate loop at the end.
    VB Code:
    1. For x = 1 to 20
    2.    Load Shape1(x)
    3.    With Shape1(x)
    4.         .Top = 20
    5.         .Left = x * 10
    6.    End With
    7. Next
    8.  
    9. For x = 1 to 20
    10.    Shape1(x).Visible = true
    11. Next
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

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