在c#中动态生成事件?
我在c#中生成了一组链接标签。我想要的是用URL填充textBox1,每个linkLabel的URL都不同。如何生成动态事件?这是一个例子:在c#中动态生成事件?,c#,winforms,events,controls,C#,Winforms,Events,Controls,我在c#中生成了一组链接标签。我想要的是用URL填充textBox1,每个linkLabel的URL都不同。如何生成动态事件?这是一个例子: foreach (var node in nodes) { HtmlAttribute att = node.Attributes["href"]; HtmlAgilityPack.HtmlDocument tempDoc = new HtmlAgilityPack.HtmlDocument(); tempDoc.LoadHtml(
foreach (var node in nodes)
{
HtmlAttribute att = node.Attributes["href"];
HtmlAgilityPack.HtmlDocument tempDoc = new HtmlAgilityPack.HtmlDocument();
tempDoc.LoadHtml(node.InnerHtml);
var tempNode = tempDoc.DocumentNode.SelectSingleNode("//img[@alt]");
HtmlAttribute tempAtt = tempNode.Attributes["alt"];
LinkLabel ll = new LinkLabel();
ll.Location = new Point(20, 20 * i);
ll.Text = tempAtt.Value;
this.Controls.Add(ll);
i++;
}
节点文本应该是
tempAtt.Value
,单击文本框1时应该填充att.Value
您不能直接将dat传递给事件,您必须从处理程序内部以其他方式获取它
foreach (var node in nodes)
{
...
LinkLabel ll = new LinkLabel();
...
ll.Click += MyLabelClickHandler;
this.Controls.Add(ll);
i++;
}
void MyLabelClickHandler(object sender, Eventargs e)
{
senderLabel = sender as LinkLabel;
string text = senderlabel.text;
....
}
请不要删除并重新发布。您可以编辑问题。