Drag and Drop with multiple ListViews

I am performing a drag and drop between any of four to six ListView controls on my form. The drag and drops work fine except for one thing - the items remain in the ListView that the drag is *from*.

I have seen several code examples for how to do this and they all have TWO ListViews and therefore hard-code which control to remove the items from.

For example, I have ListView1 through ListView6. The user drags from ListView2 and drops into ListView3. The items appear correclty in ListView3 but are not removed from ListView2.

Any thoughts on how I would know where to remove the entries from

(Sender seems to be giving me the drop sender not the drag sender)

Thanks for any assistance!



Answer this question

Drag and Drop with multiple ListViews

  • keithv

    2 Events for the source

    Use GiveFeedback event to let the source know that your on a droppable control.

    Use the QueryContinueDrag is see if you have to complete the drag operation. This happens when the mouse or keyboard has change after the drag operation has started.

    For more detail explanation;

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.givefeedback.aspx


  • Mike D

    Where you actually able to make this work

    I added code to the QueryContinueDrag as follows:

    Private Sub ContactsListView_QueryContinueDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.QueryContinueDragEventArgs) Handles ContactsListView.QueryContinueDrag

    ' If the user dropped, then remove the entry from here

    If e.Action = DragAction.Drop Then

    ' Delete each selected entry

    For Each lvItem As ListViewItem In ContactsListView.SelectedItems

    lvItem.Remove()

    Next

    End Sub

    Now the code correctly deletes the item, but the drop now has nothing to drop. So the item disappears from the first list but does not appear in the second list.

    Any other thoughts here


  • cshields

    You are correct, the source Query Event happens before the destination DragDrop event.

    The other way is to use the same logic that you have before, just make sure that you pass the ListViewItem object as the data for the drag an drop. When the DragDrop event is fired. You can use the object which is the data(e.Data.GetData(), you can cast it to ListViewItem) to delete itself. If you need to drag multiple items, then you can use the Collection class under MsVisualBasic namespace or other list-type classes.

  • Drag and Drop with multiple ListViews