添加标签面板编程 [英] Add label to Panel programmatically

查看:155
本文介绍了添加标签面板编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个表格,我想用一些控件(标签,单选按钮)加载窗体时。结果
和我想从代码做到这一点,当然加入一些面板(它是用于与测试的应用程序,而这些问题将是随机的)结果
这是我做了什么至今:

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.

推荐答案

添加刚刚创建的Form.Controls收集面板

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

    }
}

这篇关于添加标签面板编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆