By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,117 Members | 2,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,117 IT Pros & Developers. It's quick & easy.

Drag&Drop for multiple items

P: n/a
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

Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

"Dolorous Edd" <ed******************@tin.it> wrote in message
news:dp********************************@4ax.com...
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

Nov 20 '05 #2

P: n/a
On Mon, 15 Dec 2003 01:19:57 GMT, "Lloyd Sheen"
<sq*******************@tostopspamhotmail.com> wrote:
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!
Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.