Thursday, April 16, 2020

[C#][Example] Dynamic create LinkLabel and related event handler

place in constructor
Dictionary<String,String> pdfs = new Dictionary<String, String>(){
  {"Apple","http://www.example.com/apple.pdf"},
  {"Banana","http://www.example.com/banana.pdf"},
  {"Orange","http://www.example.com/orange.pdf"},
};
LinkLabel[] labels = new LinkLabel[pdfs.Length];
for(KeyValuePair<string, string> pair in pdfs){
  label[i] = new Label();
  label[i].Name = "label"+i;
  label[i].Location = new Point(27,i*30);
  label[i].TabStop = true;
  label[i].Text = pair.Key;
  label[i].Links[0].LinkData = pair.Value;
  this.Controls.Add(labels[i]);
}
And the Event Handler
private void linkedLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e){
  System.Diagnostics.Process.Start(e.Link.LinkData as string);
}
Referecne:
https://www.dotnetperls.com/dictionary

No comments :

Post a Comment