使用代码将pictureBox添加到面板

时间:2016-05-13 14:36:48

标签: c# winforms panel picturebox

我在Visual Studio / windows窗体应用程序中有一个面板。但我不能用code.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;


        }
    }

1 个答案:

答案 0 :(得分:0)

您可以使用panelName.Controls.Add(temp),我建议您更改PictureBoxfor loop的顶部以查看所有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;

    }
}