首页 > 解决方案 > 如何将面板添加到另一个面板

问题描述

当我单击 button1 时,我想将其中一个panel1,2,... 添加到 panel0 中。

在此处输入图像描述

这是我的代码

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

在此处输入图像描述

有建议吗?

标签: c#formspanel

解决方案


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

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

推荐阅读