Strange Behavior with Pictureboxes

I am experiencing strange behavior with PictureBoxes and have narrowed down a test case.

I have four PictureBoxes on my test form. Two have a background color set ... one red, one blue:

enter image description here

If I add the following code, the red Picturebox correctly parents itself to the upper Picturebox:

        this.redPictureBox.Parent = this.pictureBox1;
        this.redPictureBox.Location = this.pictureBox1.Location;
        this.redPictureBox.Height = this.pictureBox1.Height;
        this.redPictureBox.Width = this.pictureBox1.Width;

This works as expected:

enter image description here

However, if I add code to do the exact same thing with the blue PictureBox, nothing happens. In fact, it appears that the second PictureBox from the top disappears altogether:

        this.bluePictureBox.Parent = this.pictureBox2;
        this.bluePictureBox.Location = this.pictureBox2.Location;
        this.bluePictureBox.Height = this.pictureBox2.Height;
        this.bluePictureBox.Width = this.pictureBox2.Width;

enter image description here

Why is this behavior occuring? I must be missing something obvious but the code between the two is identical ... so why the different behavior?

Jon Skeet
people
quotationmark

I suspect this is the problem:

this.bluePictureBox.Location = this.pictureBox2.Location;

You're setting the location of the blue picture box within picture box 2 to be the location of picture box 2 relative to the container. I suspect you want:

this.bluePictureBox.Location = new Point(0, 0);

The only reason this wasn't much of an issue for the red picture box is that picture box 1 is near the top of the screen. Even so, you can see that it's not the full height/width that it was, and it's not at the top-left of picture box 1.

people

See more on this question at Stackoverflow