views:

2297

answers:

7

Hi all.

This one is very weird:

My app works just fine, but suddenly the damn ListView control's events are not raised any more. It just comes and goes without any clear reason. (Obviously) I've set the AllowDrop property to True and handled the DragEnter, DragOver and DragDrop events as follows:

Private Sub lstApplications_DragDrop(ByVal sender As Object, ByVal e As    System.Windows.Forms.DragEventArgs) Handles lstApplications.DragDrop, Me.DragDrop
    m_fileNames = CType(e.Data.GetData(DataFormats.FileDrop), String())
    mnuType.Show(Cursor.Position, ToolStripDropDownDirection.BelowLeft)
End Sub

Private Sub lstApplications_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lstApplications.DragEnter, Me.DragEnter, lstApplications.DragOver, Me.DragOver
    If chkMode.Checked OrElse Not e.Data.GetDataPresent(DataFormats.FileDrop, True) Then
        e.Effect = DragDropEffects.None
    Else
        e.Effect = DragDropEffects.Copy
    End If
End Sub

It doesn't matter what code I wrote in these two methods because none of the events are raised. Is there anything I'm missing here?


I run the same app on another machine and it worked just fine. I then restarted my own machine and everything started working again. I'm not sure, but seems like something was wrong with Windows.

A: 

Does the form itself have its AllowDrop property set to true?

Judah Himango
Yes. It is weird that it works for a while, but breaks suddenly.
TheAgent
Is something maybe changing the value of AllowDrop somewhere in your code?
lc
I checked. The property value is True and intact.
TheAgent
A: 

Have you tried deleting the method handler stubs, recompiling, then putting them back it and compiling again? I know it sounds wacky but there have been times were I just needed to reset my working copies.

Ian Jacobs
+1  A: 

If you're getting intermittent behavior it may be possible that an exception is escaping on of your handlers and an inadvertently disconnecting drag drop. Try adding a blanket Try/Catch block around your code and do a Debug.Fail in the Catch block. That will at least rule out the possibility of an unhandled exception being your problem.

JaredPar
+2  A: 

Just remembered we have indeed seen this before, long time ago.

I believe it happens like this:

Drag and drop works fine until some user code throws an exception during a drag and drop operation.

The exception will be eaten; you won't get any error dialog (try it yourself and see). After this point, the drag and drop will stop working.

Judah Himango
It didn't start working even when I closed the app and run it again. I don't think it is about an exception being thrown. But I'm not sure.
TheAgent
+1  A: 

It's possible that your control's creation is being munged, somehow. Are you certain that the handle exists and that the full set of initialization code has run?

I recently encountered a similar problem where Control.OnHandleCreated wasn't being run due to a mistake I made, and this resulted in all kinds of badness, including exactly what you're describing here.

Greg D
+1 I just corrected a bug with the same symptomps, i.e. drag and drop stopped working. Turned out I had overridden OnHandleCreated but failed to call the base implementation. This also had the weird effect that the DragEnter event went to the parent control instead.
Wim Coenen
+1  A: 

I've had this problem when running Visual Studio 2008 on Windows 7. VS2008 must run with admin privileges on Windows 7, so I'm running it as a different user. I'm pretty sure this prevents drag 'n' drop working as the application runs fine when run as an app, but drag 'n' drop won't work when being run from Visual Studio.

RB
A: 

This post was the answer for me: http://stackoverflow.com/questions/2833709/c-drag-drop-does-not-work-on-windows-7