0

I created a control (called Table) made up by two pictureBoxes and two Labels.

I'm trying to drag and drop it from a panel to another, but it doesn't work. This is my code:

    void TableExampleMouseDown(object sender, MouseEventArgs e)
    {
        tableExample.DoDragDrop(tableExample, DragDropEffects.Copy);
    }

    void Panel2DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Copy;
    }

    void Panel2DragDrop(object sender, DragEventArgs e)
    {
        panel2.Controls.Add((Table) e.Data.GetData(e.Data.GetFormats()[0]));
    }

Obviously I've set AllowDrop to true in panel2. Already when I click on Table object (which is in panel1), the mouse cursor doesn't change. It looks like the MouseDown event doesn't fire...

Thank you!

This is the part of the constructor code in which I subscribe Handlers:

        this.tableExample.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TableExampleMouseDown);
        this.label2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Label2MouseDown);
        this.panel1.DragDrop += new System.Windows.Forms.DragEventHandler(this.Panel1DragDrop);
        this.panel1.DragEnter += new System.Windows.Forms.DragEventHandler(this.Panel1DragEnter);
Pietro
  • 29
  • 8
  • So place a [**breakpoint**](https://msdn.microsoft.com/en-us/library/5557y8b4.aspx) in `MouseDown` and see if it does fire or not. – Visual Vincent Aug 07 '17 at 09:31
  • I've tried and I saw that it does not fire... Instead, if I do the same thing on a standard control such as a TextBox, it correctly works... – Pietro Aug 07 '17 at 10:12
  • So you don't seem to actually subscribe to the event... See my answer below. – Visual Vincent Aug 07 '17 at 10:23
  • 1
    What are the odds that you actually click on one of those pboxes or labels? Which fires *their* MouseDown event, not the UserControl's. You'll have to fix that by subscribing all of the MouseDown event for those controls and call this.OnMouseDown(). Painting a glyph that attracts the user to click on to start the drag would be wise. Much the same for the panel, the odds that its events fire get low when you add controls to it. Do note that you set AllowDrop on the wrong control, "panel1" does not seem to have much to do with the events for a control named "Panel2". – Hans Passant Aug 07 '17 at 10:31
  • See the update on my answer. – Visual Vincent Aug 07 '17 at 10:34
  • @HansPassant : That's just what I wrote in my answer. Great minds think alike. ;) – Visual Vincent Aug 07 '17 at 10:35

1 Answers1

0

You seem to have forgot to subscribe to the MouseDown event. Simply writing an event hanlder isn't enough.

Put this in the Form_Load event handler or in the form's constructor:

tableExample.MouseDown += new MouseEventHandler(TableExampleMouseDown);

For more information refer to the documentation: How to: Subscribe to and Unsubscribe from Events - Microsoft Docs.


EDIT:

It could also be that you press one of the child controls of your custom control. Child controls have their own MouseDown events.

To make the child controls also raise the parent control's MouseDown event put this in the constructor of your custom control:

MouseEventHandler mouseDownHandler = (object msender, MouseEventArgs me) => {
    this.OnMouseDown(me);
};
foreach(Control c in this.Controls) {
    c.MouseDown += mouseDownHandler;
}

EDIT 2:

Based on the new code you added to the question you seem to have forgotten to subscribe to the events for panel2:

this.panel2.DragDrop += new System.Windows.Forms.DragEventHandler(this.Panel2DragDrop);
this.panel2.DragEnter += new System.Windows.Forms.DragEventHandler(this.Panel2DragEnter);
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • Ok, thank you. It probably was a distraction error. Anyway now the MouseDown event fires, but DragEnter and DragDrop still not. Or better, if I Drag and Drop a label on panel2 they correctly work, but when I try to drag tableExample, still nothing... – Pietro Aug 07 '17 at 10:33
  • @Pietro : Could you share some more code in your question? For instance the form constructor so we can see which events you subscribe to. – Visual Vincent Aug 07 '17 at 10:40
  • Anyway now it works thanks to what you said me to add. Thank you so much!! – Pietro Aug 07 '17 at 11:03
  • I did it. I still have just a little problem... Also by using DragDropEffects.Copy, my control gets removed from the source. Isn't there any difference with Move?? Thanks – Pietro Aug 07 '17 at 12:19
  • @Pietro : The `DragDropEffects` is, like the name says, an _**effect**_. It only indicates what the _**mouse/cursor**_ should look like while dragging. -- A control can only be in one container at a time. If you want a copy of it you have to clone it via [**`Control.MemberwiseClone()`**](https://msdn.microsoft.com/en-us/library/system.object.memberwiseclone(v=vs.110).aspx) or: https://stackoverflow.com/questions/10266589/clone-controls-c-sharp-winform – Visual Vincent Aug 07 '17 at 14:18