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

时间:2019-12-15 14:02:15

标签: c# winforms picturebox

我想在Picturebox的顶部显示一个PictureBox。我有一个“母亲”图片框,旁边有一个按钮。每次单击按钮时,新的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()和

  

调用父级控件集合的GetChildIndex和SetChildIndex方法。

我也尝试过。我不知道如何称呼它,或者它根本不起作用。寻找解决方案的时间过长。有人知道如何在PictureBox顶部显示该PictureBox吗?

2 个答案:

答案 0 :(得分:0)

原来,我忘记了将图片框添加到“母亲”图片框。我加了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);

不需要提出来,我忘了添加它...

答案 1 :(得分:-1)

您需要向表单控件添加新的图片框

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