0

I'm trying to create an array/list of picturebox objects that are declared and added to the form on button click (Meaning that I'm not creating multiple objects with my array, but plan to if I can get this to run). Not getting errors, but the pictureboxes themselves do not appear on the form.

private void spawn_Click(object sender, EventArgs e)
{
    var pictureTest[0] = new PictureBox();

    pictureTest[0].Image = Properties.Resources.testimage;
    pictureTest[0].Location = new Point(500, 250);
    pictureTest[0].Name = "spawn1";
    pictureTest[0].Size = new Size(50, 50);
    pictureTest[0].TabIndex = 98;
    pictureTest[0].TabStop = false;

    this.Controls.Add(pictureTest[0]);

}

Through the course of my research, I've mainly just gotten the advice to use this.Controls.Add, but that doesn't seem to be my issue here. My array is declared earlier with:

PictureBox[] pictureTest = new pictureTest[100];
Paul Michaels
  • 16,185
  • 43
  • 146
  • 269
Four
  • 1
  • 2
  • What does Visual Basic have to do with this? You know VB and C# are not the same thing, right? – Broots Waymb Mar 16 '18 at 13:23
  • Ugh, meant visual studio, probably should have put windows form. Kind of a mess right now, I'll see if I can figure out how to edit – Four Mar 16 '18 at 13:25
  • If you put a breakpoint on the first line of `spawn_Click` and then Run with Debugging, does the breakpoint get hit? And check the Window size, maybe the window size < the position where you put the PictureBox so it 'appears' out of view. – Peter B Mar 16 '18 at 13:29
  • It does, and I remember trying this earlier and seeing that the properties were getting set correctly with a watch window. – Four Mar 16 '18 at 13:32
  • The form size is 630, 550 and I'm setting the picture box to 500, 250 – Four Mar 16 '18 at 13:33
  • Hard-coding [0] does not make sense when you want multiple boxes. Use a List or add a variable that keeps track of the count. Giving them all the same Location is why it looks like there is only one, they are on top of each other. – Hans Passant Mar 16 '18 at 14:09
  • I'm only attempting to create one to test whether I can get an array to work at all. It doesn't look like there is only one, nothing shows up at all. @HansPassant – Four Mar 16 '18 at 14:13
  • Consider setting the BackColor to Yellow so you can verify that the image is good. Call BringToFront() to ensure it isn't being overlapped by another control. Use a smaller Location so you can be sure it isn't beyond the client area of the form. And always post the code you actually tried, this can't compile. – Hans Passant Mar 16 '18 at 14:18

1 Answers1

0

As @HansPassant says in the comments, this code doesn't compile. The following should broadly do what you want:

int _position = 10;

private void spawn_Click(object sender, EventArgs e)
{

    var pictureTest = new PictureBox();

    pictureTest.Image = Properties.Resources.testimage;
    pictureTest.Location = new Point(_position, 250);
    pictureTest.Name = "spawn1";
    pictureTest.Size = new Size(50, 50);
    pictureTest.TabIndex = 98;
    pictureTest.TabStop = false;

    this.Controls.Add(pictureTest);

    _position += 100;
}

Firstly, there's no need to maintain a separate array of PictureBox, as they are part of your form. Secondly, as @HansPassant said - you were overlaying the images directly on top of each other, so you couldn't tell if you had 1 or 1000.

Paul Michaels
  • 16,185
  • 43
  • 146
  • 269