views:

314

answers:

2

Why do we actually need to handle DragEnter event of the drop destination?

What is its effect at the destination?

At Source

public partial class ToolBoxForm : System.Windows.Forms.Form
    {
     public ToolBoxForm()
     {
      InitializeComponent();
     }

     private void lbl_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
     {
      Label lbl = (Label)sender;
      lbl.DoDragDrop(lbl.Image, DragDropEffects.Link);
     }
    }

At Destination:

public partial class DrawingArea : Form
    {
     public DrawingArea()
     {
      InitializeComponent();
     }

     private void DrawingArea_Load(object sender, System.EventArgs e)
     {
      ToolBoxForm toolBoxForm = new ToolBoxForm();
      this.AddOwnedForm(toolBoxForm);
      toolBoxForm.Show();

      pictureBox1.AllowDrop = true;
     }

     private void picDrawingArea_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
     {
            if (e.Data.GetDataPresent(DataFormats.Bitmap))
            {
                e.Effect = DragDropEffects.Copy;                
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
     }

     private void picDrawingArea_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
     {
      Graphics g = pictureBox1.CreateGraphics();
            g.DrawImage((Image)e.Data.GetData(DataFormats.Bitmap), new Point(e.X - this.Left - 12, e.Y - this.Top - 30));
     }
    }

When I am commenting out the code:

if (e.Data.GetDataPresent(DataFormats.Bitmap))
                {
                    e.Effect = DragDropEffects.Copy;                
                }
                else
                {
                    e.Effect = DragDropEffects.None;
                }

The image is not being dropped.

+2  A: 

From to the DragDropEffects MSDN Page:

Member
name
Description
None The drop target does not accept the data.
Copy The data from the drag source is copied to the drop target.
Move The data from the drag source is moved to the drop target.
Link The data from the drag source is linked to the drop target.
Scroll The target can be scrolled while dragging to locate a drop position that is not currently visible in the target.
All The combination of the Copy, Move, and Scroll effects.

So you've got to set it something other than None if you want to accept the drop.

However, the next quote led me to believe that it was just used for feedback:

You can use DragDropEffects to display different mouse pointers for drag-and-drop operations. For example, you can display a plus symbol for a Copy drag-and-drop operation, an arrow symbol for a Move drag-and-drop operation, or a red circle with a line through it symbol for a None drag-and-drop operation.

ChrisF
But when I am commenting out the code, the image is not being dropped on the drop area. Plz, see the update.
JMSA
I believe if you don't switch away from DragDropEffects.None, you can't handle the drop event. This means you have to switch it to something like DragDropEffects.Copy if you want to know when the user drops what they were dragging. Could be wrong...
Adam A
If I call e.Data.GetDataPresent(DataFormats.Bitmap) and don't change the cursor, then the image is not being dropped on the drop area. That is, same problem.
JMSA
And I have created another VS2005 sln. I am dragging a node from treeview to a DataGridView. The same problem is happening if i do not handle this event. I think, this is essential in all cases. Have you come up with your own test?
JMSA
If you set e.Effect to None, you are telling the D+D plumbing that you don't accept the drop. The default is None so commenting out your code disables the drop.
Hans Passant
+1  A: 

I really think the Drop event won't fire if DragDropEvents is left at None (which is the default). So that's why the image doesn't drop.

Adam A