日期:2014-05-18  浏览次数:20985 次

请教: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);

}

------解决方案--------------------
注册事件啊...