Flaw in code for dragdrop listviews


B

Bernie Yaeger

Following is the code for a drag/drop between 2 listviews (smallicon view).
It has one serious flaw that I can't seem to solve: it allows either
listview to drag and drop inside itself, thus duplicating the item:
Private Sub ListView_ItemDrag(ByVal sender As Object, ByVal e As
System.Windows.Forms.ItemDragEventArgs) Handles ListView1.ItemDrag,
ListView2.ItemDrag

Dim myitem As ListViewItem

Dim myitems(sender.SelectedItems.Count - 1) As ListViewItem

Dim i As Integer = 0

For Each myitem In sender.SelectedItems

myitems(i) = myitem

i = i + 1

Next

sender.dodragdrop(New DataObject("System.Windows.Forms.ListViewItem()",
myitems), DragDropEffects.Move)

End Sub

Private Sub ListView_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter,
ListView2.DragEnter

If e.Data.GetDataPresent("System.Windows.Forms.ListViewItem()") Then

e.Effect = DragDropEffects.Move

Else

e.Effect = DragDropEffects.None

End If

End Sub

Private Sub ListView_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop,
ListView2.DragDrop

Dim myitem As ListViewItem

Dim myitems() As ListViewItem =
e.Data.GetData("System.Windows.Forms.ListViewItem()")

Dim i As Integer = 0

For Each myitem In myitems

sender.Items.Add(myitems(i).Text)

If sender Is ListView1 Then

ListView2.Items.Remove(ListView2.SelectedItems.Item(0))

Else

ListView1.Items.Remove(ListView1.SelectedItems.Item(0))

End If

i = i + 1

Next

End Sub

Any ideas how I can get this problem fixed?

Thanks for any help.

Bernie Yaeger
 
Ad

Advertisements

Ad

Advertisements

B

Bernie Yaeger

Hi Sean,

Thanks for the idea, but it doesn't work - I tried it in the dragenter,
dragdrop events, and a variant in the dragitem event - still doesn't prevent
the duplication inside a given listview. But I appreciate the effort.

Bernie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top