简体   繁体   English

PictureBox中的DragDrop-从不调用DragEnter

[英]DragDrop in PictureBox - DragEnter never gets called

I want use DragDrop in my PicureBox es but DragDrop() and DragEnter() methods are never called. 我想在PicureBox使用DragDrop ,但是从不调用DragDrop()DragEnter()方法。

I created method MouseMove and in this method I called DoDragDrop() which should call DragDrop() and DragEnter() . 我创建了MouseMove方法,并在此方法中调用了DoDragDrop() ,该方法应调用DragDrop()DragEnter() MouseMove is called but rest not. 调用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);
   }
} 

I had a similar issue. 我有一个类似的问题。 The problem is that you have AllowDrop for the form, but not the picture. 问题是您有AllowDrop ,但没有图片。 And for a reason I ignore, AllowDrop is not a member of PictureBox . 由于我忽略的原因, 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实例。

myPictureBox.AllowDrop = true;

尽管在myPictureBox的方法列表中看不到“ AllowDrop”,您仍可以使用此代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM