Here's the code generated when you add a PictureBox to a form in the designer: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:
<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 ClassVB 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)




Reply With Quote