我想知道如何为标签数组中的每个数组元素分配一个eventHandler。我了解无法为每个eventHandler创建一个方法,那么解决方案是什么?谢谢!
for(int i = 0, i < 10; i++)
{
lbs[i] = new Label();
lbs[i].Location = new System.Drawing.Point(76 + f, 164);
lbs[i].Size = new System.Drawing.Size(49, 17);
//able to perform this, but wont able to create a method for this
lbs[i].Click += new System.EventHandler(lbs[i]_Click);
}
//can't do this, what is alternative?
public void lbs[i]_Click(object sender, EventArgs e)
{
}
最佳答案
您的函数名称无效(函数名称中不能包含[]
),请尝试将lbs[i]_Click
更改为lbs_Click
。
for(int i = 0; i < 10; i++)
{
lbs[i] = new Label();
lbs[i].Location = new System.Drawing.Point(76 + f, 164);
lbs[i].Size = new System.Drawing.Size(49, 17);
lbs[i].Name = "label" + i;
//able to perform this, but wont able to create a method for this
lbs[i].Click += new System.EventHandler(lbsi_Click);
}
public void lbsi_Click(object sender, EventArgs e)
{
var label = sender as Label;
if(label != null && label.Name == "label1"){
//event was raised from label1
}
}
关于c# - 为标签数组元素创建事件处理程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12480049/