如何通过编程向表单添加控件?

4

我正在尝试在面板内添加一个控件(Label)。请参见以下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AddControlProgramatically
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Label lbl = new Label();

            for (int x = 0; x <= 3; x++)
            {
                //create new label location after each loop
                //by multiplying the new value of variable x by 5, so the new label 
                //control will not overlap each other.
                lbl.Location = new System.Drawing.Point(52 + (x * 5), 58 + (x * 5));
                //create new id and text of the label
                lbl.Name = "label_" + x.ToString();
                lbl.Text = "Label " + x.ToString();

                this.panel1.Controls.Add(lbl);
            }
        }
    }
}

截图

这是表单。我试图编程生成3个不同的控件标签,但正如你所看到的,它只显示最后一个。请帮助我解决这个问题。我知道我的代码有问题(因为它没有工作)。谢谢...


抱歉,我不知道如何接受答案...只是这个论坛的新手。具体怎么做呢? - yonan2236
当您提出问题时,在投票下方会出现一个勾选标记。要接受正确的答案,请单击该勾选标记。这不仅可以为用户赚取额外的积分,还可以让其他人知道哪个答案有效,如果他们遇到类似的问题。 - Jerod Houghtelling
4个回答

6

Label lbl = new Label();放在循环内。

并且增加偏移量,修改为...

 lbl.Location = new System.Drawing.Point(52 + (x * 5), 58 + (x * 5))

...to:

lbl.Location = new System.Drawing.Point(52 + (x * 30), 58 + (x * 30))

3

您需要在每次循环迭代中创建一个新标签。目前,您只创建了一个标签。

private void button1_Click(object sender, EventArgs e)
{
    for (int x = 0; x <= 3; x++)
    {
        Label lbl = new Label();

        //create new label location after each loop
        //by multiplying the new value of variable x by 5, so the new label 
        //control will not overlap each other.
        lbl.Location = new System.Drawing.Point(52 + (x * 5), 58 + (x * 5));
        //create new id and text of the label
        lbl.Name = "label_" + x.ToString();
        lbl.Text = "Label " + x.ToString();

        this.panel1.Controls.Add(lbl);
    }
}

看代码,似乎应该创建4个标签。(x <= 3)。 - Jerod Houghtelling
我希望我在编程方面和你一样厉害。去年我开始学习编程,是因为我的朋友。我不是IT人员,但我觉得编程非常有趣和有意思。现在我正在进行自学,并选择学习C#语言。我的朋友也借给我一些关于编程的书籍。(我想我可能要转行了):) - yonan2236
先生,它已经可以工作了...只是间距的问题导致其他标签无法显示,它们重叠在一起了...我非常感谢您的回复。再次感谢。 - yonan2236
@yonan2236 回头看,间距似乎不够,很抱歉我没注意到!坚持下去,随着你对编程语言的熟悉度越来越高,它会变得更容易! - Jerod Houghtelling

0
非常古老的问题,但显然没有人花时间把它搞对...... 您一直在覆盖同一个Label对象实例。相反,创建一个Label实例列表,然后像这样将它们添加到您的表单中:
        List < Label > myLabels = new List<Label>();
        for (int i = 0; i < 5; i++)
        {
            Label lbl = new Label();                
            //create new id and text of the label
            lbl.Name = "label_" + i.ToString();
            lbl.Text = "Label " + i.ToString();
            lbl.Width = 50;
            lbl.Location = new System.Drawing.Point(52 + (i * lbl.Width), 50);
            myLabels.Add(lbl);
        }
        foreach (Label l in myLabels)
        {
            this.Controls.Add(l);
        }

0

你需要将 Label lbl = new Label(); 放在你的 for 循环内部。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接