Create picboxes via code

etherias

Member
Joined
May 5, 2010
Messages
5
Programming Experience
1-3
Sorry if this has been asked/answered somewhere in the forums already, but I could not find it =s

How can I create picture boxes using code? The application that I am making, the amount of picture boxes needed for a certain thing is not known, and is always changing (and im planing to put them into a picture box array).

Also, how can I set the new picture box (assuming that its possible with code) to have the same properties/size etc as another picturebox? Is it possible for me to set a "mold" or something?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,251
Location
Sydney, Australia
Programming Experience
10+
All controls are actually created in code. When you design a form it simply generates that code for you. You can open the designer code file from the Solution Explorer if you click the Show All Files button and then expand node for your form. You can then examine the code that the designer generates and write similar code yourself. Basically, you create an instance of the desired class, set the appropriate properties to the desired values and then Add it to the Controls collection of the appropriate container, e.g. the form or a Panel.

That said, laying out such controls can be a bit tedious. Depending on the circumstances, you may be best to use a TableLayoutPanel or a FlowLayoutPanel as the container, in which case layout will be handled automatically.

It's also important to note that you handle events slightly differently when you create the controls at run time. This may not be an issue for PictureBoxes but I'll cover it anyway. When you handle events of controls added in the designer, the event is added to the Handles clause of the method. If you create controls yourself then you must write the event handler yourself and then use the AddHandler statement to attach it to the event at run time. If you use the same event handler for multiple controls, you determine which control raised the event using the 'sender' parameter.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,251
Location
Sydney, Australia
Programming Experience
10+
As for the last question, you should write a method that configures the control. You can then simply call that method and pass a newly created control. If you do that multiple times then each control will be configured the same way. If you want to use another control as a template then you pass them both in and assign property values from one to the other.
 

etherias

Member
Joined
May 5, 2010
Messages
5
Programming Experience
1-3
Me.PictureBox1 = New System.Windows.Forms.PictureBox
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()

Me.PictureBox1.BackgroundImage = Global.Forest_Wars.My.Resources.Resources.Player_Interface
Me.PictureBox1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch
Me.PictureBox1.Location = New System.Drawing.Point(-1, 343)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(746, 130)
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False


This was essentially what the designer code was, but how come it doesnt work? How do I set it so that it knows its inside a specific form?
 

etherias

Member
Joined
May 5, 2010
Messages
5
Programming Experience
1-3
After googling "Adding controls during runtime", I ended up with a code that works


Public Class GameScreen
Dim label1 As New Label()

Private Sub GameScreen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
label1.Size = New Size(200, 20)

label1.Location = New Point(75, 40)

label1.Text = "Label 1 is added on run time"
Me.Controls.Add(label1)
End Sub
End Class

Thanks for the help!
 
Top Bottom