Skip to content

How to make linklabel work

henrik

Asked Jul 17, 2007, 1:35 AM EDT2 answers
Share
LinkedInX
im trying to make a list of linklabels the link to all linklabels is the same exept an idnumber from MyClass but i am out of ideas how to make it work. does anyone know what i need to make it work ? ArrayList list = new ArrayList(); private LinkLabel[] lutt = new LinkLabel[100]; int t=0; foreach (MyClass s in list) { lutt[t] = new LinkLabel(); lutt[t].Location = new System.Drawing.Point(10, 80 + (tl * 25)); lutt[t].Name = ""; lutt[t].Text = "more"; this.Controls.Add(lutt[t]); t=t+1; }

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Mike Gold
    0
    Looks like you were pretty close.  By work, did you mean activating the Click event?  If so, here is one version of the code:

    C#
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    CreateMyClassList();
    CreateLinkLabels();
    }
    private void CreateMyClassList()
    {
    list.Add(new MyClass(1));
    list.Add(new MyClass(2));
    list.Add(new MyClass(3));
    list.Add(new MyClass(4));
    }
    private LinkLabel[] lutt = new LinkLabel[100];
    ArrayList list = new ArrayList();
    public void CreateLinkLabels()
    {
    int t=0;
    foreach (MyClass s in list)
    {
    lutt[t] = new LinkLabel();
    lutt[t].Location = new System.Drawing.Point(10, 80 + (t * 25)); lutt[t].Name = "link" + s.ID;
    lutt[t].Text = "more " + s.ID;
    lutt[t].Click += new EventHandler(Form1_Click);
    this.Controls.Add(lutt[t]);
    t=t+1;
    }
    }
    private void Form1_Click(object sender, EventArgs e)
    {
    switch (((LinkLabel)sender).Name)
    {
    case "link1":
    MessageBox.Show("pressed link 1");
    break;
    case "link2":
    MessageBox.Show("pressed link 2");
    break;
    default:
    break;
    }
    }
    }
    0
  • 0
    thanx a lot it was exactly what i was trying to do , i was totally stock
    0