主题:[讨论]看看我写的建立动态控件代码错在哪?!
如下是我写的一个关于建立动态建立Label控件的代码,就是能显示“第1道题”和“第2道题”错在哪?
private void button1_Click(object sender, EventArgs e)
{
int n;
n = int.Parse(textBox1.Text);
Label[]lb=new Label[n];
lb[0] = new Label();
lb[0].Top = 50;
lb[0].Left = 50;
lb[0].Text = "第1道题";
for (int i = 1; i < n; i++)
{
lb[i]=new Label();
lb[i].BackColor = Color.White;
lb[i].Top = lb[i - 1].Top -lb[i-1].Height;
lb[i].Left = lb[i - 1].Left;
lb[i].Text = "第" + (i - 1) + "道题";
}
this.Controls.AddRange(lb);
}
private void button1_Click(object sender, EventArgs e)
{
int n;
n = int.Parse(textBox1.Text);
Label[]lb=new Label[n];
lb[0] = new Label();
lb[0].Top = 50;
lb[0].Left = 50;
lb[0].Text = "第1道题";
for (int i = 1; i < n; i++)
{
lb[i]=new Label();
lb[i].BackColor = Color.White;
lb[i].Top = lb[i - 1].Top -lb[i-1].Height;
lb[i].Left = lb[i - 1].Left;
lb[i].Text = "第" + (i - 1) + "道题";
}
this.Controls.AddRange(lb);
}