Question

I have a TreeView whose AllowDrop property is set to false but still shows the copy cursor when dragging a file over it. I've read about the QueryContinueDrag event but that seems to only fire when dragging things already on the object/form/whatever be around. I would like to cancel the drag operation i.e. return the cursor to normal and drop the file, when a user takes a file from outside the application (desktop, email attachment, etc) and brings it into the TreeView. Are there any ways I can do this? From what I can tell the QueryContinueDrag is the only event with an Arg that allows me to set the DragAction to "cancel" and it doesn't fire when bringing in outside files. I've also considering forcing an "esc" keypress or mouse right click but I don't want to use PIvoke.

Was it helpful?

Solution

@elgonzo is correct, it is not your responsibility to cancel a drag and drop operation that you did not start. You only need to concern yourself with whether or not you intend to allow the data to be dropped, if it is attempted. By the sounds of it, you don't, so they won't be able to drop the data.

If you're really concerned about the cursor, then you can change that from several event handlers for events like PrevieMouseMove or PreviewDragOver using this:

Mouse.SetCursor(Cursors.No);

For your information though, you can set the DragEventArgs.Effects property to DragDropEffects.None which will automatically set the cursor for you. From the DragEventArgs.Effects Property page on MSDN:

By default, the effect specified in this property determines the mouse cursor for the target of a drag-and-drop operation. This is useful to provide feedback to the user on the operation that will occur when the corresponding object is dropped.

You can access the DragEventArgs object from any of the following events:

PreviewDragOver, DragOver, PreviewDrop, Drop, PreviewDragEnter, DragEnter, PreviewDragLeave, DragLeave

So all you need to do is to show that data cannot be dropped, by setting the DragEventArgs.Effects property to DragDropEffects.None in one of these handlers. I'd recommend one of the continuous ones like PrevieMouseMove or PreviewDragOver.

OTHER TIPS

You can write :

if (condition) {
      `e.Handled = true;
       ((Thumb)e.OriginalSource).CancelDrag();
       return;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top