Search code examples
c#winformsfunctionlinklabel

How to find out the name of the caller object during a function?


I'm trying to create a "list" using linklabels to identify attachments (in a mail client). So, I've this to create the links:

Label newLabel = new LinkLabel();
newLabel.Name = "anexo" + Convert.ToString(anexos_file.Count); //anexos_file is a list where all the attachments Paths exist
newLabel.Text = Path.GetFileName(file);
newLabel.Left = bt_anexos.Left;
newLabel.Top = label2.Top;
newLabel.Width = 150;
newLabel.AutoSize = true;
newLabel.Click += new System.EventHandler(Click_anexo); //Click_anexo is the name of the function

Now I need to know how do I make a function that, when I click the link, deletes the link itself.

So, any help?


Solution

  • private void Click_anexo(object sender, EventArgs arg)
    {
    
    }
    

    Object sender parameter contains information about the control which fired this event. Cast sender as Label

    LinkLabel lbl = (LinkLabel)sender;
    

    and use it

    lbl.Visible = false;
    

    I think making it invisible is as good as deleted.