object array?

  • Thread starter Thread starter davep15
  • Start date Start date
D

davep15

My visual basic knowledge is a bit out of date. The last version I
significantly used was VB6 and now I'm trying to do something with
2005.

I want to create 106 copies of an PictureBox
(System.Windows.Forms.PictureBox) and I want them to be indexed so I
can easily access them from a loop. In vb6 I'd just create 1 set it to
have an index of 0 in the properties window then use the load command
to create as many as I want but that doesn't appear to be working
here. How would I do this with VisualStudio2005
 
Hello, Dave,

VB dotNet provides a PictureBoxArray that (I think) is supposed to help
replace the functionality you are missing. But I have never found any need
to use this. Whenever I have to do something similar, I just declare an
array of the appropriate type and populate it with the controls that I want.
I have found this to be much more useful than the old VB6 control arrays.
That is, I would do something like the (untested) code below:


Dim PicBoxArray(106) As PictureBox
For intPicture As Integer = 0 To PicBoxArray.GetUpperBound(0)
PicBoxArray(intPicture) = New PictureBox
PicBoxArray(intPicture).Top = ...
PicBoxArray(intPicture).Left = ...
PicBoxArray(intPicture).Width = ...
PicBoxArray(intPicture).Height = ...
PicBoxArray(intPicture).property1 = PicBoxTemplate.property1
PicBoxArray(intPicture).property2 = PicBoxTemplate.property2
" " " " "
" "
Me.Controls.Add(PicBoxArray(intPicture))
Next intPicture

where "property1", "property2", etc. would be replaced by any properties
that you want to set to non-default values.

Cheers,
Randy
 
Dave,

Be aware that there are in the sample of Omega 107 picturebox references
(one not used).

Because a strange compatible desision with VB6 is an declared array in VB
always 1 more.
(To keep it compatible with the indexing starting at 1 and not as most
classes uses in Net at 0)

Cor
 
Hi Randy,

Why do you use in your sample. 0 to PicBoxArray.GetUpperBound(0) while you
use the constant 106 to declare.

In this case it cost only (not so much) time.

In my idea is simpe 0 to 105 enough

Just to make you attent on this probably use from the past.

Cor
 
You are right, I should have written the array property "length", which is
not a function.
 
Ah thank you, it was the
Me.Controls.Add(PicBoxArray(intPicture))
That I was missing, I moved your Dim PicBoxArray(106) to outside the
function as private PicBoxArray(0 to 105) and it now does what I need,
I need to be able to change the images latter from different
functions. Thank you

P.S. It treated your example as the quoted text from my message and
hid it, leading me to almost post a what example question here.
 
Hoi, Cor,

Actually, the 106 was intentional. Dave's example also includes 107
PictureBox references. (He is making 106 copies of the original PictureBox.)

Groetjes,
Randy
 
Is there a way to get the the object's events (click, mouse move, etc)
I remember in vb6 there was 1 function for each event that was used
for the entire array and the index was simply a calling argument. I'd
love to be able to do something similar here.
 
Is there a way to get the the object's events (click, mouse move, etc)
I remember in vb6 there was 1 function for each event that was used
for the entire array and the index was simply a calling argument. I'd
love to be able to do something similar here.

Try this as a way of doing what you are asking.

1st. Create a new UserControl called MyPictureBox. Change the interits
in the designer .vb file so that it inherits PictureBox.

2nd. Create your event handling in the new class.

3rd. Where you load the PictureBox(es) change the code to load your new
control. Then you have a simple method to create code that will be executed
for every control.

Hope this helps

Lloyd Sheen
 
Hi, Dave,

Lloyd's method of overriding the On... methods in an inherited class will
work very well, and in many cases may be the best approach.

As an alternative (or if you prefer to not replace the standard PictureBox
controls with your own custom version) you can simply add a common event
handler to each of the PictureBox controls in the array. Here is an example
(actually tested this time ;-) ).

Cheers,
Randy


Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Const kinPBHeight As Integer = 60
Const kinPBWidth As Integer = 80
Const kinVerticalOffset As Integer = 10
Const kinHorizontalOffset As Integer = 10

Dim PicBoxArray(5) As PictureBox
For intPicture As Integer = 0 To PicBoxArray.GetUpperBound(0)
PicBoxArray(intPicture) = New PictureBox
PicBoxArray(intPicture).Top = kinVerticalOffset + _
intPicture * (kinPBHeight +
kinVerticalOffset)
PicBoxArray(intPicture).Left = Button1.Right + kinHorizontalOffset
PicBoxArray(intPicture).Width = kinPBWidth
PicBoxArray(intPicture).Height = kinPBHeight
PicBoxArray(intPicture).BorderStyle = BorderStyle.FixedSingle
PicBoxArray(intPicture).Name = "Dynamically added picture box #"
& intPicture + 1
Me.Controls.Add(PicBoxArray(intPicture))
' Here is where you add the event handler.
AddHandler PicBoxArray(intPicture).Click, AddressOf
CommonClickHandler
Next intPicture
End Sub

Private Sub CommonClickHandler(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Dim pbxSender As PictureBox = DirectCast(sender, PictureBox)
MsgBox(pbxSender.Name & " has been clicked.")
End Sub

End Class
 
Thanks Omega I think that will get me most of what I want.
Now I just have to decide if I should pick the index number out of the
name or if I should inherit the class to create 1 more data member to
store the index from the start.
 
Thanks Omega I think that will get me most of what I want.
Now I just have to decide if I should pick the index number out of the
name or if I should inherit the class to create 1 more data member to
store the index from the start.


I think what you might need to concider is which part of your application
needs to respond to events etc from the pictureboxes. If each picturebox is
a representation of an object that your application uses then what I posted
the first time plus adding a property to associate the picturebox (your
picturebox) with an object.

Then the picturebox can handle its own events within the context of the
object. This allows for much freedom in that you can have more than one
type of object associated with each picturebox and if they share inheritance
or an interface then your code can become trivial.

Something like (psuedo code)

Interface IMyInterface
Sub IveBeenClicked

Class MyPictureBox
public property MyObject as IMyInterface


Create mypicturebox(es)
mypicturebox.myobject = ctype(itsAssociatedObject,IMyInterface)
add to GUI


In MyPictureBox event (lets say click)

me.myobject.IveBeenClicked

Hope this helps

Lloyd Sheen
 
Add in the procedure from Randy

AddHandler PicBoxArray(intPicture).Click, AddressOf myPictureBox_Click
Me.Controls.Add(PicBoxArray(intPicture))

Add in the method(the event)

Dim currentPictereBox as PictureBox = Directcast(Sender,PictureBox)

Written in this message so watch typos

Cor
 
Back
Top