读书人

请问:C#中利用循环添加的控件如何给控

发布时间: 2012-07-27 11:03:01 作者: rapoo

请教:C#中利用循环添加的控件怎么给控件添加事件 比如Click事件
for (int i = 1; i <=10;i++ )
{

for (int j = 1; j <=8; j++)
{
Label l = new Label();
l.Text = i + "-" + j;
l.Width = 53;
l.Height = 20;
l.Location = new Point((j-1) * 60 + 53, (i-1) * 25 + 20);
l.Font = new Font(this.Font.FontFamily, 12);
l.TextAlign = ContentAlignment.MiddleCenter;
l.BackColor = Color.Yellow;
this.Controls.Add(l);
}

}

//给每个Lable添加Click事件

[解决办法]
l.Click+= 后按下Tab

for (int j = 1; j <=8; j++)
{
Label l = new Label();
l.Text = i + "-" + j;
l.Width = 53;
l.Height = 20;
l.Location = new Point((j-1) * 60 + 53, (i-1) * 25 + 20);
l.Font = new Font(this.Font.FontFamily, 12);
l.TextAlign = ContentAlignment.MiddleCenter;
l.BackColor = Color.Yellow;
l.Click+=
this.Controls.Add(l);

}

[解决办法]
注册事件啊...

读书人网 >C#

热点推荐