Results 1 to 3 of 3

Thread: How to create pictureboxes at runtime?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Posts
    88

    How to create pictureboxes at runtime?

    What is the code to create pictureboxes at runtime?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to create pictureboxes at runtime?

    Here's the code generated when you add a PictureBox to a form in the designer:
    VB Code:
    1. <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    2. Partial Class Form1
    3.     Inherits System.Windows.Forms.Form
    4.  
    5.     'Form overrides dispose to clean up the component list.
    6.     <System.Diagnostics.DebuggerNonUserCode()> _
    7.     Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    8.         If disposing AndAlso components IsNot Nothing Then
    9.             components.Dispose()
    10.         End If
    11.         MyBase.Dispose(disposing)
    12.     End Sub
    13.  
    14.     'Required by the Windows Form Designer
    15.     Private components As System.ComponentModel.IContainer
    16.  
    17.     'NOTE: The following procedure is required by the Windows Form Designer
    18.     'It can be modified using the Windows Form Designer.  
    19.     'Do not modify it using the code editor.
    20.     <System.Diagnostics.DebuggerStepThrough()> _
    21.     Private Sub InitializeComponent()
    22.         [U]Me.PictureBox1 = New System.Windows.Forms.PictureBox[/U]
    23.         CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
    24.         Me.SuspendLayout()
    25.         '
    26.         'PictureBox1
    27.         '
    28.         [U]Me.PictureBox1.Location = New System.Drawing.Point(0, 0)[/U]
    29.         [U]Me.PictureBox1.Name = "PictureBox1"[/U]
    30.         [U]Me.PictureBox1.Size = New System.Drawing.Size(100, 50)[/U]
    31.         [U]Me.PictureBox1.TabIndex = 0[/U]
    32.         [U]Me.PictureBox1.TabStop = False[/U]
    33.         '
    34.         'Form1
    35.         '
    36.         Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
    37.         Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    38.         Me.ClientSize = New System.Drawing.Size(292, 266)
    39.         [U]Me.Controls.Add(Me.PictureBox1)[/U]
    40.         Me.Name = "Form1"
    41.         Me.Text = "Form1"
    42.         CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
    43.         Me.ResumeLayout(False)
    44.  
    45.     End Sub
    46.     [U]Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox[/U]
    47.  
    48. End Class
    Note that it declares a PictureBox variable, creates an object by invoking the PictureBox constructor using the 'New' keyword, then sets the appropriate properties. That's exactly the same as for any object. The only difference with a control is that you have to add it to the form's Controls collection to have it displayed on the form. You just need to write the same code to have a PictureBox displayed at run time. You declare a variable, create an object, set the appropriate properties and add the control to the form:
    VB Code:
    1. Dim pb As New PictureBox
    2.  
    3. pb.Location = New Point(100, 100)
    4. pb.Size = New Size(200, 200)
    5. pb.Image.Load("file path here")
    6. Me.Controls.Add(pb)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Posts
    88

    Re: How to create pictureboxes at runtime?

    Worked very nicely. One more question: Can I make an array by using that method? If I'm adding onto the tail of my worm in Pac-man, I'm going to need multiple pictures. Therefore, could I do something along the lines of:

    VB Code:
    1. Dim picTail1(24) As New PictureBox
    2.         picTail1.Location = New Point(26, 27)
    3.         picTail1.Size = New Size(26, 27)
    4.         Me.Controls.Add(picTail(0))

    I know that doesn't work, but I'm just giving you an idea of what I want to do.

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