-1

I am doing program with Drag&Drop labels added by button to FlowLayoutPanel. It works all fine, but I added ContextMenu with only one item for deleting selected label. When right click on label and select "delete" I want to dispose this specific label. It was working but now it is not.

void fillFLP(FlowLayoutPanel FLP)
        {

            Label l = new Label();

            l.AutoSize = true;
            l.Text = textBox.Text;
            l.BackColor = Color.Red;
            l.Width = 150;
            l.AutoSize = true;
            l.MaximumSize = new Size(150, 200);
            l.MinimumSize = new Size(150, 25);
            l.Padding = new Padding(5);
            l.Margin = new Padding(25, 5, 25, 0);
            ContextMenuStrip deleting= new ContextMenuStrip();
            deleting.Items.Add("Delete");
            deleting.Click += new System.EventHandler(this.delete);
            l.ContextMenuStrip = deleting;

            FLP.Controls.Add(l);
            l.MouseDown += l_MouseDown;
            l.MouseMove += l_MouseMove;
            l.MouseUp += l_MouseUp;
        }

        private void delete(object sender, EventArgs e)
        {
            this.Dispose();
        }

Thank you for any help.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • Be careful here. The term "dispose" has a very specific meaning in .NET. You're using it as a synonym for "delete", but disposal and deletion are **not** the same thing. – Daniel Mann Jan 04 '15 at 00:55
  • do a google search on how to use / implement IDisposable or how to use Dispose.. – MethodMan Jan 04 '15 at 00:56
  • I know, I read something about it, but this was working when it was in CustomControl class. But I need it in this combination. – Jakub Knytl Jan 04 '15 at 00:58
  • do you understand the code that was in the CustomControl Class as well as what you stripped out..? debug the 2 and you will probably find your issue / difference in regards to what you are missing.. – MethodMan Jan 04 '15 at 01:04
  • 1
    Looks like you just copied the code from the ControlClass to the form event. `this` refers to something altogether different in those two contexts, no? It would not hurt to expand on what is "not working" too. – Ňɏssa Pøngjǣrdenlarp Jan 04 '15 at 01:04
  • Hans' answer is elegant and correct. However you should also understand how to fix the code you actually wrote. Change the Delete event like this: `private void delete(object sender, EventArgs e) { ((ContextMenuStrip)sender).SourceControl.Dispose(); }` – TaW Jan 04 '15 at 08:15

1 Answers1

2

You are disposing the wrong object of course. It needs to be l, can't get to it because you made it a local variable. The most elegant way is to capture it with a lambda expression or an anonymous method. Note how poor name choices got you in trouble as well, you are subscribing the wrong Click event. Fix:

    ContextMenuStrip cms = new ContextMenuStrip();
    var deleting = cms.Items.Add("Delete");
    deleting.Click += delegate { l.Dispose(); };
    l.ContextMenuStrip = cms;
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thank you very much! I was trying with "l" but I had no idea about delegating. – Jakub Knytl Jan 04 '15 at 01:17
  • Hans: How is he `subscribing the wrong Click event`? The event is fine, it is only referring to the wrong object! See my comment above..! You are right about the name choices; I'm not [completely inncocent here ;-)](http://stackoverflow.com/questions/27750427/c-sharp-drag-and-drop-labels-within-flowlayoutpanels/27754110#27754110) – TaW Jan 04 '15 at 08:19