使用代码将图片框添加到面板

Adding pictureBox to panel with codes

我有一个 Visual Studio/windows 形式的面板 app.But 我无法在上面添加 pictureBox code.It 如果我没有面板工作但我需要 it.MyCodes ]:

      PictureBox[] pipe = new PictureBox[3];

 private void Form1_Load(object sender, EventArgs e)
    {
        CreatePipes(1);}

    private void CreatetopPipes(int Number)
    {
          for (int i = 0; i <= Number; i++)
        {

            PictureBox temp = new PictureBox();
            this.Controls.Add(temp);
            temp.Width = 50;
            temp.Height = 350;
            temp.BorderStyle = BorderStyle.FixedSingle;
            temp.BackColor = Color.Red;
            temp.Top = 30;
            temp.Left = 300;
            topPipe[i] = temp;
            topPipe[i].Visible = true;


        }
    }

您可以使用panelName.Controls.Add(temp),我建议您在for loop中更改PictureBox的顶部以查看所有PictureBox,如下所示:

private void CreatetopPipes(int Number)
{
    for (int i = 0; i <= Number; i++)
    {

        PictureBox temp = new PictureBox();
        panelName.Controls.Add(temp);
        temp.Width = 50;
        temp.Height = 350;
        temp.BorderStyle = BorderStyle.FixedSingle;
        temp.BackColor = Color.Red;
        temp.Top = temp.Height * panelName.Controls.Count;
        temp.Left = 300;
        topPipe[i] = temp;
        topPipe[i].Visible = true;

    }
}