DragDrop question

  • Thread starter Thread starter Owe Armandt
  • Start date Start date
O

Owe Armandt

When I play with DragDrop of files to a form I get two DragEnter events
before the DragDrop event, why is this??


Owe
 
Hi Owe,

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that when you do a drag drop from a
windows explorer folder onto the form, the Drag_Enter event will be fired
twice before the Drag_Drop fired.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

I can not reproduce the problem, here is my reproduce code.(I tested on
windows xp+sp1, vs.net 2003)
<code snippet>
private void Form1_DragEnter(object sender,
System.Windows.Forms.DragEventArgs e)
{
if(e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect=DragDropEffects.All;
}
else
{
e.Effect=DragDropEffects.None;
}
System.Diagnostics.Debug.WriteLine("Form1_DragEnter");
}

private void Form1_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)
{
string[] paths = (string[])e.Data.GetData("FileDrop",true);
if (paths.Length>0)
this.Text = paths[0];
System.Diagnostics.Debug.WriteLine("Form1_DragDrop");

}
</code snippet>

You may try to create a new windows application and set the AllowDrop
property to true, and then add two event handlers, one is the dragenter,
the other is dragdrop.
Try to make a test to see if you can reproduce the problem, or can you
simplied your code to reproduce the problem and post here?
Or you may try to see if you have advised the dragenter event twice which
may cause the problem.

Please apply my suggestion above and let me know if it helps resolve your
problem.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
I noticed one diff in your suggestion against my code - I'm doing
MessageBox.Show("DragEnter") and MessageBox.Show("DragDrop") respectively.
That is the difference, if I use MessageBox I get two DragEnter events - any
clue to why thata happens??



Owe
 
Hi Owe,

If we show the messagebox in the DragEnter and DragDrop, the focus will be
switched between the windows application and the messagebox.
When we drag a file onto the form , the DragEnter will be fired, and in the
meantime, the messagebox will got the focus, if we move the mouse outside
the form's area, the mouse_leave event will be fired, and after we click
the Ok button of the messagebox, the focus will be switch back to the form,
and now the form think another drag_enter fired.

You may make a test, if we show the form and move it in the center of the
desktop, so that when the messagebox is shown, it is right above the form.
And then when we click the ok button of the messagebox, the mouse_leave
event will not be fired, and now the drag_enter will not be fired twice.

If you use the Spy++ shipped with VS.NET to spy on the Form window, you
will find that what had happened.

So I do not think that we show a messagebox in a dragenter will be a good
practice, because this will cause the focus switch which will messy the
default behavior.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
That is absolutely fine with me, have no need for that MessageBox - just had
it for a test and did not realize that it would have that effect.
Thank you for your explanation.


Owe
 
Back
Top