mnn
mnn

Reputation:

PictureBoxes and Zorder [.NET]

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!

Upvotes: 2

Views: 1977

Answers (2)

Daniel LeCheminant
Daniel LeCheminant

Reputation: 51091

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.

Upvotes: 2

Neil N
Neil N

Reputation: 25268

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.

Upvotes: 0

Related Questions