views:

361

answers:

2

Hello, I have difficulties with showing multiple (up to 5) pictureboxes on top each other, but able to see all of them. Only first and last will show up. I show them from bottom to top, set their locations correctly and using BringToFront() function. Does anybody have a solution (if any)? Thanks for help!

+2  A: 

Not sure what the actual problem is; the following code adds 5 stacked PictureBoxes to a form, and configures them so they are all visible.

private void AddStackedPictureBoxes()
{
    for (int i = 0; i < 5; i++)
    {
        PictureBox pb = new PictureBox();

        pb.BackColor = Color.FromArgb(i * 50, i * 50, i * 50);
        pb.BorderStyle = BorderStyle.FixedSingle;

        pb.Location = new Point(i * 10, i * 10);
        pb.Size = new Size((5 - i) * 20, (5 - i) * 20);

        Controls.Add(pb);

        pb.BringToFront();
    }
}

Perhaps that will give you some insight into why you are having problems.

For example, if you call BringToFront() before the control is added to the form, it won't have any effect.

Daniel LeCheminant
thanks, the problem was that I added them *after* using BringToFront() ... didn't realized until now (bad habit from VS Form Desinger - set all properties, then add to panel/form etc.)
You also need to make sure your pictureboxes are added to the same control. I was going crazy wondering why a picturebox added to the main form wouldn't go under a picturebox added to a panel. Whoops!
ashes999
A: 

Be sure to set background color to transparent on each of them.

and as Daniel said, BringToFront() only works if the control is already on a form.

Neil N
well I don't know why I should do that (with transparent color). I draw pictures with no transparent regions/areas whatsoever.
Well I assumed the reason you'd stack them is so that you can have a layered effect.
Neil N