Question Creating pictureBox Array at run time

genesch

Member
Joined
Apr 15, 2013
Messages
5
Programming Experience
10+
I've poked around the internet and this forum and can't get a clear answer to this:

I'm trying to get an array of pictureBox(es) to appear on the form. Nothing I try seems to work, so.. any help would be appreciated. I va esomething like this:

Dim invaders(4) As PictureBox
invaders(0) = Me.picAlien1
invaders(1) = Me.picAlien2
invaders(2) = Me.picAlien3
invaders(3) = Me.picAlien4

Dim x As Integer
For x = 0 To invaders.Length - 1
invaders(x) = New PictureBox <<< === says it's needed, but seems to be counterproductive
invaders(x).Left = x * 40
invaders(x).Top = 40
invaders(x).Image = Image.FromFile("SIbottom0.gif")
Next

This isn't really what I want anyway, but better than nothing, which is what I got now.. Ideally, what I would like is a dynamic array of picture boxes that can be created at run time, so basically something like this:

Dim invaders(4) As PictureBox
Dim x As Integer
For x = 0 To invaders.Length - 1
invaders(x) = New PictureBox
invaders(x).Left = x * 40
invaders(x).Top = 40
invaders(x).Image = Image.FromFile("SIbottom0.gif")
Next






Any help would be greatly appreciated..

thanks...
 
If you want a control that you create at run time to appear on the form then you have to add it to the Controls collection of the appropriate parent, e,g, the form itself or a Panel or GroupBox.
 
You need to do what I said in my previous post. If you want to see how controls are created then you should look at the code you already have that has been generated by the designer. Open the Solution Explorer, click the Show All Files button, expand a form and then open the Designer.vb file. It contains all the code generated by your actions in the designer.
 
I am using VB Express, so I don't have Solution Explorer or Designer.vb file, os if you don't mind, please tell me what code I need to add to make this visible on the form.

If you're referring to adding this line inside the for loop:
Me.Controls.Add(invaders(x))

I've tried it and it doesn't seem to work.

I've tried everything I could think of, so I'm pretty much dead in the water. Again, if you could post what code actually makes this work, I'd really appreciate it.

Thanks...
 
I am using VB Express, so I don't have Solution Explorer or Designer.vb file, os if you don't mind, please tell me what code I need to add to make this visible on the form.
Um, yes you do and yes you do. I suggest you look a bit harder.
If you're referring to adding this line inside the for loop:
Me.Controls.Add(invaders(x))

I've tried it and it doesn't seem to work.
That's exactly what I mean and it will work if you do it properly. If it didn't work then you didn't do it properly but you haven't shown us what you did or told us what happened so we can only guess at what the issue is. My guess is that you never actually created a PictureBox in the first place. Just because you have an egg carton doesn't mean that you have any eggs. Likewise, having a PictureBox array doesn't mean that you have any PictureBoxes. You still have to create the PictureBoxes and put them in the array.
 
Ok.. something I tried in the past that didn't work all of a sudden is working.. let me do some more investigation..
I'm guessing that what you think you tried before is not what you actually tried and what you're trying now is actually different. Again, it's just an educated guess, but I reckon that you didn't actually create the PictureBox objects by invoking a constructor with the New keyword.
 
Actually, I did (see original post at the top).. and I was wrong.. I cleaned up the code to do only what I want, and it didn't work again.. So here's what I'm trying to do. I've put this code in the _Load sub procedure (so, maybe this is the wrong place to put it?):

Dim invaders(4) As PictureBox
Dim x As Integer
For x = 0 To invaders.Length - 1
invaders(x) = New PictureBox
invaders(x).Left = x * 40
invaders(x).Top = 40
invaders(x).Image =Image.FromFile("SIbottom0.gif")
Me.Controls.Add(invaders(x))
Next

The image is in the resources, so everything works as intended when it's just 1 image, but not when it's created dynamically. BTW, I'm not trying to find an easy answer because I'm lazy.. I've been programming for ~30 years, but I'm extending my VB knowledge as I'm trying to teach it to my students (never having taken a single VB class myself), and right now am running into a brick wall. I've tried everything I know or can think of, so I would really appreciate a direct answer rather than some vague hints (and the book I'm using doesn't have anything useful).

I also have radio button controls that will change the image based on which button is clicked, but I will deal with that once I can get them to appear on the screen.

Thanks...

 
You seem to be confused on a few points. First of all, you say:
The image is in the resources
but there's nothing in that code that's using a resource. There's a fair chance that that is the root of your problem. I suspect that what's actually happening is that an exception is being thrown in that code but, because the code is in the Load event handler, the exception is being swallowed and execution just picks up at the end of the method. The first thing to do is to determine whether that is the case or not. If it is then the second thing to do is to determine what's causing the exception and fix it. So, wrap that code in a Try...Catch block and see if an exception gets caught. If it does, interrogate it to get all the information you can about the issue.
 
Back
Top