Delete programmatically created Labels

Asked

Viewed 100 times

0

I’m developing a mapping application that uses Abels to name the different rooms in a building.

The code I use to create the Abels is as follows:

private void button2_Click(object sender, EventArgs e)
{  
            Label l = new Label();
            l.Left = l.Left;
            l.Top = l.Top;
            l.BringToFront();
            l.BackColor = Color.Transparent;
            l.Text = (listBox1.SelectedItem).ToString();
            l.BringToFront();
            l.Visible = true;
            l.Name = "label_";
            // Add whatever other properties you deem important

            l.Size = new Size(250, 20);

            this.Controls.Add(l);
            {
                l.BringToFront();
            }

            Controls.Add(l);
            ControlExtension.Draggable(l, true);
}

I tried to use the following to remove what I wanted, but just remove the initial label and not the ones that are created in Runtime:

private void l_DoubleClick(object sender, EventArgs e)
{
    this.Controls.Remove(l);
}

My last attempt was:

public void Label_Killer(Label label)
{
    label.Dispose();
}

to then call in the creation of a new label but do not know how to proceed...

So I come here to ask: Is there any way to remove any label by double click?

Thank you.

1 answer

1


You need to add the event DoubleClick in the honeycombs it creates. After raising the label, use the code:

l.DoubleClick += L_DoubleClick;

And create the event:

private void L_DoubleClick(object sender, EventArgs e)
{
    this.Controls.Remove(((Label)sender));
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.