0

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?

TaW
  • 53,122
  • 8
  • 69
  • 111
  • 1
    A normal approach would be to replace the picture in the picture box. Are you sure you want one on top of it? – Stefan Dec 15 '19 at 14:09
  • 1
    Add `newPictureBox.Parent = oldPbox;` – TaW Dec 15 '19 at 14:21
  • ... or `momPictureBox.Controls.Add(newPictureBox);` as you'ld do with any other control. – Jimi Dec 15 '19 at 15:16
  • @Stefan Yep. I need it like that. I just need the element. The Graphics inside aren't important. I use the "mother" Picturebox basicallay as Layout so the User knows, where he/she/it can operate. @TaW never thought of that. This works too. I added ```planConfigDraw.Controls.Add(newPictureBox);``` instead. Both works. Thank you. @Jimi yep that did it thank you <3 – Johannes Roß Dec 16 '19 at 16:26

2 Answers2

0

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...

-1

you need to add new picturebox to form's controls

PictureBox NewPictureBox = new PictureBox();
NewPictureBox.BackColor = Color.Red;
NewPictureBox.Location = MotherPictureBox.Location;
NewPictureBox.Size = MotherPictureBox.Size;
this.Controls.Add(NewPictureBox);
NewPictureBox.BringToFront();
Mena Samer
  • 122
  • 1
  • 11