如何找到linkLabel被点击?

时间:2017-11-20 18:39:44

标签: c# winforms textbox linklabel

这是一种接受Dictonary的方法。

 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中将显示字典值?

1 个答案:

答案 0 :(得分: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
    }
}