我试图在面板中添加一个控件(标签)。 请参阅代码:如何以编程方式将控件添加到窗体?
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个不同的控制标签。但正如你所看到的,它只显示最后一个。请帮助我解决这个问题。我知道我的代码有问题(因为它不工作)。谢谢...
对不起,我不知道如何接受answers..just在新手这个论坛。如何做到这一点? – yonan2236 2010-08-02 02:08:10
当你问一个问题时,在向上/向下投票下面有一个复选标记。要接受正确答案,请点击复选标记。它给予用户额外的点数,并让其他人知道哪些答案有效,如果他们有类似的问题。 – 2010-08-02 02:18:10
谢谢先生... – yonan2236 2010-08-02 02:20:28