0

这是一些接受字典的方法。

 public void Display(Dictionary<string, string> feeds)
        {
            for (int i = 0; i < 5; i++)
            {
                int x = i + 1;
                string numberOfLable = "linkLabel" + x;
                var l = Controls.Find(numberOfLable, true).First() as LinkLabel;
                string text = feeds.ElementAt(i).Key;
                l.Text = text;


                //TO DO add click event for link label
                //that display value from Dictionary
            }

        }

如何添加如果单击 linkLabel[i],则在 textBox 中将显示字典值的代码?

4

1 回答 1

0

要将链接标签链接到事件,只需

l.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(LinkLabelClicked);

LinkLabelClicked 代码看起来像这样

private void LinkLabelClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    LinkLabel label = sender as LinkLabel;
    string key = label.Text;
    if (feeds.TryGetValue(key, out string value))
    {
        myTextBox.Text = value;
    }
    else
    {
        //do something to complain about the error
    }
}
于 2017-11-21T02:53:10.970 回答