Results 1 to 5 of 5

Thread: creating picture box controls at run-time

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    creating picture box controls at run-time

    Please could you tell me how to create a picture box at run-time. What i want to do is right click on the form, and have a pop menu come up. From the menu click create a picture control to be added to form. Can you create controls like this at run-time

    Many thanks in advance
    steve

  2. #2
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    You sure can! It goes something like this.
    VB Code:
    1. Option Explicit
    2. Dim WithEvents picNew As PictureBox
    3.    
    4. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    5.    If Button = vbRightButton Then
    6.       Me.PopupMenu mnuPop
    7.    End If
    8. End Sub
    9.  
    10. Private Sub mnuPopCreate_Click()
    11.  
    12.    Set picNew = Controls.Add("vb.Picturebox", "picNew")
    13.    
    14.    With picNew
    15.       .Left = 400
    16.       .Top = 600
    17.       .Visible = True
    18.       .BackColor = vbRed
    19.    End With
    20.    
    21. End Sub

  3. #3
    Addicted Member
    Join Date
    Nov 2000
    Location
    State of Confusion
    Posts
    133
    I could be mistaken, but that would only allow you to add one more PictureBox, then you would get an error saying that a control with the name "picNew" already exists...

    I've been adding them this way.... Then I can add as many as I needed to.....

    Code:
    Private Sub Command1_Click()
    Dim X As Integer
      X = picNew.Count
      Load picNew(X)
        With picNew(X)
          .Left = X * 100
          .Top = X * 100
          .Visible = True
        End With
    End Sub
    The only problem I've had with this is that you already have to have one PictureBox on your form at DesignTime with the "Index" property set to "0"....
    "Don't take life too seriously, you'll never get out alive"
    from Van Wilder

  4. #4
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606
    CadDragon:
    me too i like it better this way,
    that way if you want to put code for onClick or something you can
    and control it with the index!!

  5. #5
    Addicted Member
    Join Date
    Nov 2000
    Location
    State of Confusion
    Posts
    133
    Yeah, you could do something cool like have a button on a form that says "Do you like buttons?" and when they click "Yes", it it fills the users screen with buttons and have it where each button is unloaded when it's clicked according to its index, so that they have to clear out a whole lotta buttons to get back to the main form so they can close the program..... Hehe, just an idea....
    "Don't take life too seriously, you'll never get out alive"
    from Van Wilder

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