4

所以我有一个表单,我想在表单加载时添加一些带有一些控件(标签和单选按钮)的面板。
当然,我想从代码中做到这一点(它是用于制作带有测试的应用程序,问题将是随机的)
这是我到目前为止所做的:

List<Panel>ls=new List<Panel>();

private void VizualizareTest_Load(object sender, EventArgs e)
{
    for (int i = 0; i < 4; i++)
    {
        Panel pan = new Panel();
        pan.Name = "panel" + i;
        ls.Add(pan);
        Label l = new Label();
        l.Text = "l"+i;
        pan.Controls.Add(l);
        pan.Show();
    }

}

但它没有在表单上显示任何内容。

4

2 回答 2

7

将刚刚创建的面板添加到 Form.Controls 集合中

private void VizualizareTest_Load(object sender, EventArgs e)
{
    for (int i = 0; i < 4; i++)
    {
        Panel pan = new Panel();
        pan.Name = "panel" + i;
        ls.Add(pan);
        Label l = new Label();
        l.Text = "l"+i;
        pan.Location = new Point(10, i * 100);
        pan.Size = new Size(200, 90);  // just an example
        pan.Controls.Add(l);
        this.Controls.Add(pan);

    }
}
于 2013-03-13T12:56:50.323 回答
0

在此处输入图像描述

private void button2_Click(object sender, EventArgs e)
    {
        int X = 153;
        int Y = 34;
        for (int i = 1; i < 4; i++)
        {
            Panel pnl = new Panel();
            pnl.SuspendLayout();
            pnl.Location = new Point(X, Y);
            pnl.Name = "pnl"+i;
            pnl.Size = new Size(200, 57);
            pnl.BorderStyle = BorderStyle.FixedSingle;

            Label lbl = new Label();
            lbl.Location = new Point(X - 100, Y - 17);
            lbl.Name = "lbl" + i;
            lbl.Size = new Size(75, 23);
            lbl.Text = "lable_" +i;

            pnl.Controls.Add(lbl);
            pnl.ResumeLayout(false);

            this.Controls.Add(pnl);

            Y = Y + 95;
        }
    }

why not display label2 & label3?
于 2020-06-15T11:20:13.917 回答