|
-
Oct 27th, 2006, 08:41 PM
#1
Thread Starter
Lively Member
How to create pictureboxes at runtime?
What is the code to create pictureboxes at runtime?
-
Oct 27th, 2006, 08:53 PM
#2
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:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
[U]Me.PictureBox1 = New System.Windows.Forms.PictureBox[/U]
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'PictureBox1
'
[U]Me.PictureBox1.Location = New System.Drawing.Point(0, 0)[/U]
[U]Me.PictureBox1.Name = "PictureBox1"[/U]
[U]Me.PictureBox1.Size = New System.Drawing.Size(100, 50)[/U]
[U]Me.PictureBox1.TabIndex = 0[/U]
[U]Me.PictureBox1.TabStop = False[/U]
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 266)
[U]Me.Controls.Add(Me.PictureBox1)[/U]
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
[U]Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox[/U]
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:
Dim pb As New PictureBox
pb.Location = New Point(100, 100)
pb.Size = New Size(200, 200)
pb.Image.Load("file path here")
Me.Controls.Add(pb)
-
Oct 27th, 2006, 09:02 PM
#3
Thread Starter
Lively Member
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:
Dim picTail1(24) As New PictureBox
picTail1.Location = New Point(26, 27)
picTail1.Size = New Size(26, 27)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|