Drag&Drop for multiple items

D

Dolorous Edd

Hi,
for a program I'm working on I need to be able to drag multiple files
between Windows Explorer and a ListBox, in both directions.

Implementing the "drag in" was pretty easy, but I can't find a way to
export more than on item at time.

The ideal result would be to be able to drag the selected files, but
as soon as I click on the LB to do the drag a new selection is made
that cancels the current one and by searching with google it seems
that the problem has no simple solution (btw, I'd be glad to be proven
wrong on this)

Anyway, since I normally need to drag every element, i decided to make
the drags automatically select every element, but it seems to work
only with a single file, and I can't understand why.

The code that works is the following:

Dim targetFiles(lbFiles.SelectedIndices.Count - 1) As String
Dim i As Integer = 0
Dim J As Integer
For J = 0 To dvFiles.Count - 1
If lbFiles.GetSelected(J) = True Then
targettFiles(i) = dvFiles.Item(J).Row.Item("FileName")
i += 1
End If
Next
Dim data As New DataObject(DataFormats.FileDrop, targetFiles)
DoDragDrop(data, DragDropEffects.Copy)

lbFiles is my ListBox and dvFiles is the DataView associated with it.

This was made for the multiple selectione, hence the "for" part, and
since the selection is always single it works.
To make it drag every file, I simply changed the way targetFiles is
created, by filling it with all the elements, and the result is the
following:

Dim targetFiles(dvFiles.Count - 1) As String
Dim J As Integer
For J = 0 To dvFiles.Count - 1
targetFiles(J) = dvFiles.Item(J).Row.Item("FileName")
Next
Dim data As New DataObject(DataFormats.FileDrop, targetFiles)
DoDragDrop(data, DragDropEffects.Copy)


If I drag on Windows Explorer with this version doesn't happen
anything, and if I drag on Nero Burning Rome (the real purpose of the
project) it crashes.

I also tried to define data as an array, and add a file per element,
or to make a DoDragDrop for every file, but it didn't work either.

Does anybody has an idea of how to make it work?

Thak yon in advance
 
L

Lloyd Sheen

This is code (5 mins old and working). It takes from a listview but should
work same for listbox.

Private Sub lvwSongs_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lvwSongs.MouseDown
Dim lvw As ListView
lvw = sender
Dim dataObj As DataObject
dataObj = New DataObject
Dim arr() As String
ReDim arr(lvw.SelectedItems.Count - 1)

Dim li As ListViewItem
Dim i As Int16
For Each li In lvw.SelectedItems
arr(i) = li.Text
i += 1
Next
dataObj.SetData("FileDrop", arr)
lvw.DoDragDrop(dataObj, DragDropEffects.All)
End Sub

Ensure that listbox is set to handle multiple selection and that code is in
the mousedown event.
 
D

Dolorous Edd

This is code (5 mins old and working). It takes from a listview but should
work same for listbox.
Thank you very much. I still don't understand what was wrong with the
previous one, since the two look pretty much the same to me, but it
works now!
 

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