Search code examples
c#winformspicturebox

How do I display a PictureBox on a PictureBox in C#?


I want to display a PictureBox ontop of a Picturebox. I have a "mother" Picturebox and a button next to it. Everytime you click the Button a new PictureBox should be displayed on the "mother". I have created the PictureBox like this:

PictureBox newPictureBox = new PictureBox();
newPictureBox.Location = new Point(x:30,y:30);
newPictureBox.BackColor = Color.Red;
newPictureBox.Visible = true;
newPictureBox.Height = 200;
newPictureBox.Width = 200;

Now I have no idea how to display it to the user. I tried to use .Show() and

Call the GetChildIndex and SetChildIndex methods of the parent's Controls collection.

I also tried that. Either I don't know how to call it or it just doesn't work. Been searching for a solution for way too long. Does anybody have an idea of how to display that PictureBox on top of that pictureBox?


Solution

  • So turns out, I forgot to add the picture Box to the "mother" Picturebox. I added 1 line:

    motherPictureBox.Controls.Add(newPictureBox);
    

    So now my Code looks like this:

    PictureBox newPictureBox = new PictureBox();
    newPictureBox.Location = new Point(x:30,y:30);
    newPictureBox.BackColor = Color.Red;
    newPictureBox.Visible = true;
    newPictureBox.Height = 200;
    newPictureBox.Width = 200;
    motherPictureBox.Controls.Add(newPictureBox);
    

    Didn't need to bring it forward, I just forgot to add it...