如何在 C# 中的 PictureBox 上显示 PictureBox?

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

我想在 Picturebox 之上显示 PictureBox。我有一个 "mother" 图片框和旁边的一个按钮。每次单击 Button 时,"mother" 上都会显示一个新的 PictureBox。我创建了这样的 PictureBox:

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;

现在我不知道如何将它显示给用户。我尝试使用 .Show() 和

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

我也试过了。要么我不知道如何称呼它,要么它根本不起作用。一直在寻找解决方案太久了。有没有人知道如何在那个图片框的顶部显示那个图片框?

您需要将新的图片框添加到表单的控件中

PictureBox NewPictureBox = new PictureBox();
NewPictureBox.BackColor = Color.Red;
NewPictureBox.Location = MotherPictureBox.Location;
NewPictureBox.Size = MotherPictureBox.Size;
this.Controls.Add(NewPictureBox);
NewPictureBox.BringToFront();

结果是,我忘了将图片框添加到 "mother" 图片框。我添加了 1 行:

motherPictureBox.Controls.Add(newPictureBox);

所以现在我的代码如下所示:

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);

本来不用提的,就是忘记加了...