Picturebox Array (VB Express)

  • Thread starter Thread starter toby
  • Start date Start date
T

toby

Hi there,

Is it possible to create an array of picturebox controls during
run-time. I wish to create a new image/picturebox everytime a user
clicks the button on a form, and they need to be objects so that the
user can move/drag them around.


The only examples I can find dont work as I am using VB Express (2005?)

and the examples are writting in VB6.


Is anyone able to help please?


Toby.
 
At class level in your form:

Private m_pictureboxes As New ArrayList()

In the Click event handler of your button:

Dim _pic As New PictureBox

_pic.Location = New System.Drawing.Point(<x>, <y>)

_pic.Size = New System.Drawing.Size(<w>, <h>)

' set any other properties as required

' wire up the desired event handlers
AddHandler _pic.<event>, AddressOf <event_handler>

Me.Controls.Add(_pic)

m_pictureboxes.Add(_pic)

where:

<x> and <y> are the coordinates of the desired iniial position
<w> and <h> are the width and height values for the desired size
<event> is the desired event
<event_handler> is the desired event hander (predefined)

e.g.

Private Sub PictureBox_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)

CType(sender, PictureBox).BackColor = Color.CadetBlue

End Sub

If you 'add' 3 pictureboxes by clicking the button three times and then
click a picturebox, it's background colour will change.

Obviously you will have to implement a mechanism for varying the <x> and <y>
values otherwise all the pictureboxes will be drawn in the same place.
 
Thanks very much for the responses, looks like I was almost there, but
your examples have been a godsend! :-)

Thanks again.
 
Stephany said:
At class level in your form:

Private m_pictureboxes As New ArrayList()

In the Click event handler of your button:

Dim _pic As New PictureBox

_pic.Location = New System.Drawing.Point(<x>, <y>)

_pic.Size = New System.Drawing.Size(<w>, <h>)

' set any other properties as required

' wire up the desired event handlers
AddHandler _pic.<event>, AddressOf <event_handler>

Me.Controls.Add(_pic)

m_pictureboxes.Add(_pic)

where:

<x> and <y> are the coordinates of the desired iniial position
<w> and <h> are the width and height values for the desired size
<event> is the desired event
<event_handler> is the desired event hander (predefined)

e.g.

Private Sub PictureBox_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)

CType(sender, PictureBox).BackColor = Color.CadetBlue

End Sub

If you 'add' 3 pictureboxes by clicking the button three times and then
click a picturebox, it's background colour will change.

Obviously you will have to implement a mechanism for varying the <x> and <y>
values otherwise all the pictureboxes will be drawn in the same place.

Stepahnie,

How can I reference the image property of a picturebox in an array. I
would ahve assumed it was:

m_picturebox(1).image

but that brings up errors. How can I change properties of these
controls in the array?

Thanks.
 
What 'errors'?


Stepahnie,

How can I reference the image property of a picturebox in an array. I
would ahve assumed it was:

m_picturebox(1).image

but that brings up errors. How can I change properties of these
controls in the array?

Thanks.
 
Back
Top