0

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?

  • Why do you need to add the click even in this method, like what are you ultimately trying to do? Why arent you iterating over your dictionary with a foreach? What happens when the Dictionary has more/less than 5 keys? – maccettura Nov 20 '17 at 18:42
  • I use for because i need to access the label name from it, i can't do with for loop. I don't know how to access the label without foor loop. I want to do that here becuse, i need that dictionary value to set to textbox if some of labels are clicked. Can you help me – Marija Magdalena Nov 20 '17 at 19:13

1 Answers1

0

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
    }
}
lamandy
  • 962
  • 1
  • 5
  • 13