我試圖在面板中添加一個控件(標籤)。 請參閱代碼:如何以編程方式將控件添加到窗體?
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