How to find is linkLabel is clicked?

281 Views Asked by At

This is some method that accept 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
            }

        }

How to add code that if linkLabel[i] is clicked, in textBox will show value of dictionary?

1

There are 1 best solutions below

0
lamandy On

To link a link label to event, simply

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

The LinkLabelClicked code looks something like this

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