如何将面板添加到另一个面板

时间:2019-01-02 12:02:25

标签: c# forms panel

当我单击button1时,我想将 面板之一,……添加到panel0中。

enter image description here

这是我的代码

private void button3_Click(object sender, EventArgs e)
        {
            int i = 0;
            int j = 0;


            Panel[] pnl = new Panel[3];

            while ( i<2)
            {
                pnl[i].Parent = panel0;                  // erorr

                pnl[i].BackColor = Color.PeachPuff;

                pnl[i].Size = new Size(50, 10);
                pnl[i].Location = new Point(j+5,10);
                panel0.Controls.Add(pnl[i]);
                pnl[i].BringToFront();

                i++;
                j = j + 13;
            }
         }

enter image description here

有建议吗?

1 个答案:

答案 0 :(得分:0)

将Panel [] pnl =新Panel [3]更改为:

var pnl  = new List<Panel>{existingPanel1, existingPanel2, existingPanel3};