PictureBox中的DragDrop - 永远不会调用DragEnter

时间:2021-11-25 08:08:50

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

我想在我的PicureBoxes中使用DragDrop,但从不调用DragDrop()和DragEnter()方法。

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

我创建了方法MouseMove,在这个方法中我调用了DoDragDrop(),它应该调用DragDrop()和DragEnter()。调用MouseMove但不休息。

Form constructor:

表单构造函数:

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

This is create in constructor of PictureBox:

这是在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);
   }
} 

1 个解决方案

#1


5  

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.

我有一个类似的问题。问题是你有表单的AllowDrop,但没有图片。由于我忽略的原因,AllowDrop不是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.

其中myPictureBox是我的PictureBox实例。

#1


5  

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.

我有一个类似的问题。问题是你有表单的AllowDrop,但没有图片。由于我忽略的原因,AllowDrop不是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.

其中myPictureBox是我的PictureBox实例。