Results 1 to 5 of 5

Thread: Creating instances of a control at runtime

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    Fort Worth, Texas, USA
    Posts
    264

    Question

    Hello,

    Can someone help me with this problem?

    I need to create several instances of a Shape control at runtime from 1 original control.

    The application requires a new instance of the shape be created when the user pushes a command button. Each new shape will need to insert itself within an array.

    Example:

    (this doesn't work)

    Sub Command1_Click(Index as Integer)
    Count = Count + 1
    Set BoxShape(Count) = New Shape1
    End Sub

    The newly generated BoxShape(x) needs to act like any other ordinary Shape control.

    I've seen this done in the past, but I can't remember where or how to search of this.

    Thanks,

    Kenneth K. Whiteman

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    Cool Perhaps this will help..dynamic cmdButton

    'add a command button to a form dynamically at run time

    Option Explicit

    Private WithEvents btnObj As CommandButton

    Private Sub btnObj_Click()
    MsgBox "This is a dynamically added button."
    End Sub

    Private Sub Form_Load()
    Set btnObj = Controls.Add("VB.CommandButton", "btnObj")
    With btnObj
    .Visible = True
    .Width = 3000
    .Caption = "This button was created on the fly!"
    .Top = 1000
    .Left = 1000
    End With
    End Sub


    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    This assumes you already have a shape control on the form, its index is set at design time to 0, and you want subsequent shape controls to be patterned after the original.
    Code:
    Private Sub Command1_Click()
    
        Static intShapeIndex As Integer
        
        intShapeIndex = intShapeIndex + 1
        
        Load Shape1(intShapeIndex)
        Shape1(intShapeIndex).Visible = True
        Shape1(intShapeIndex).Top = Shape1(intShapeIndex - 1).Top + Shape1(intShapeIndex - 1).Height
        
    End Sub

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    Fort Worth, Texas, USA
    Posts
    264
    Wayne,
    Thanks, This is just what I was looking for!

    However,

    Thanks Marty, your method is much easier and works great!

    Ken

  5. #5
    Guest

    Just a thought.

    The bad thing about creating them from scratch is that you cannot have code in their events. Creating them from arrays, you can add them by using a Select Case statement.

    Code:
    Select Case Index
    
    Case 1:
    ' if index 1
    
    Case 2:
    ' if index 1
    
    Case 3:
    ' if index 1
    
    End Select

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