Add label to Panel programmatically

限于喜欢 提交于 2019-12-07 10:45:45

问题


So I have a form, and I want to add some Panels with some controls(labels, and radiobuttons) when the form loads.
And I want to do it from the code, of course(it's for making an application with tests, and the questions will be random)
This is what I have done till now:

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

}

But it doesn't show anything on the form.


回答1:


Add the panel just created to the Form.Controls collection

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

    }
}


来源:https://stackoverflow.com/questions/15385921/add-label-to-panel-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!