Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在c#中动态生成事件?_C#_Winforms_Events_Controls - Fatal编程技术网

在c#中动态生成事件?

在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(

我在c#中生成了一组链接标签。我想要的是用URL填充textBox1,每个linkLabel的URL都不同。如何生成动态事件?这是一个例子:

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;
    ....
}

请不要删除并重新发布。您可以编辑问题。