gpt4 book ai didi

c# - 为标签数组元素创建事件处理程序

转载 作者:行者123 更新时间:2023-11-30 22:25:03 25 4
gpt4 key购买 nike

我想知道如何为标签数组中的每个数组元素分配一个事件处理程序。我知道不可能为每个 eventHandlers 创建一个方法,那么解决方案是什么?谢谢!

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com