Jacek Grzelaczyk
Jacek Grzelaczyk

Reputation: 793

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);
   }
} 

Upvotes: 4

Views: 3176

Answers (2)

Xiaohuan ZHOU
Xiaohuan ZHOU

Reputation: 586

myPictureBox.AllowDrop = true;

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

Upvotes: 1

Timok Khan
Timok Khan

Reputation: 71

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.

Upvotes: 7

Related Questions