动态添加面板到表单

时间:2019-09-25 05:02:38

标签: c# winforms dynamic controls panel

我正在尝试根据收到的ID数量将面板添加到表单中。在运行时,面板被添加到表单中,但是只有第一个具有控件,我不知道为什么。

这是我的实际代码:

private void loadItems(List<int> ids)
{
    int id = 0,
        panelX = 10,
        panelY = -30,
        itemslblX = 15,
        itemslblY = -65,
        IDtxtX = 360,
        IDtxtY = -40,
        nametxtX = 130,
        nametxtY = -40;

    int height = (80 * ids.Count) + 50;
    this.Size = new Size(600,height);

    foreach(int c in ids)
    {
        panelY = panelY + 80;
        itemslblY = itemslblY + 80;
        terrainMenuY = terrainMenuY + 80;
        IDtxtY = IDtxtY + 80;
        nametxtY = nametxtY + 80;

        Panel panel = new Panel();
        panel.Location = new Point(panelX, panelY);
        panel.Dock = DockStyle.None;
        panel.Height = 75;
        panel.Width = 575;
        panel.BackColor = Color.Gray;
        panel.Name = ids[id]+"Panel".ToString();

        Label itemslbl = new Label();
        itemslbl.Location = new Point(itemslblX, itemslblY);
        itemslbl.Text = "Imagen            Nombre                                         ID";
        itemslbl.Height = 20;
        itemslbl.Width = 550;
        itemslbl.Name = ids[id] + "itemsLabel".ToString();

        TextBox IDtxt = new TextBox();
        IDtxt.Location = new Point(IDtxtX, IDtxtY);
        IDtxt.Height = 27;
        IDtxt.Width = 200;
        IDtxt.Text = ids[id].ToString();
        IDtxt.ReadOnly = true;
        IDtxt.Name = ids[id] + "IDtext".ToString();

        TextBox nametxt = new TextBox();
        nametxt.Location = new Point(nametxtX, nametxtY);
        nametxt.Height = 27;
        nametxt.Width = 200;
        nametxt.ReadOnly = true;
        nametxt.Name = ids[id] + "nameText".ToString();

        panel.Controls.Add(itemslbl);
        panel.Controls.Add(nametxt);
        panel.Controls.Add(IDtxt);

        this.Controls.Add(panel);
        id++;
    }
}

Here's how it seems

请提供任何建议,并感谢您的宝贵时间。

0 个答案:

没有答案