Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DragDrop in PictureBox - DragEnter never gets called

I want use DragDrop in my PicureBoxes but DragDrop() and DragEnter() methods are never called.

I created method MouseMove and in this method I called DoDragDrop() which should call DragDrop() and DragEnter(). MouseMove is called but rest not.

Form constructor:

public Form1()
{
   InitializeComponent();
   this.AllowDrop = true;
}  

This is create in constructor of PictureBox:

this.DragDrop += new DragEventHandler(ttile_DragDrop);
this.DragEnter += new DragEventHandler(ttile_DragEnter);
this.MouseMove += new MouseEventHandler(ttile_MouseMove);

And my method:

public void ttile_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
   int i = 0;
}

public void ttile_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
   int i = 0; 
}

public void ttile_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
   if (e.Button == MouseButtons.Left)
   {
      ((PictureBox)sender).DoDragDrop(sender, DragDropEffects.All);
   }
} 
like image 212
Jacek Grzelaczyk Avatar asked Feb 18 '23 11:02

Jacek Grzelaczyk


2 Answers

I had a similar issue. The problem is that you have AllowDrop for the form, but not the picture. And for a reason I ignore, AllowDrop is not a member of PictureBox.

The trick that worked for me was to replace

this.AllowDrop = True;

by

((Control)myPictureBox).AllowDrop = True;

where myPictureBox is my instance of PictureBox.

like image 142
Timok Khan Avatar answered Mar 03 '23 09:03

Timok Khan


myPictureBox.AllowDrop = true;

You can use this code though you won't see 'AllowDrop' in myPictureBox's method list.

like image 40
Xiaohuan ZHOU Avatar answered Mar 03 '23 09:03

Xiaohuan ZHOU