Drag and Drop to Windows Explorer

  • Thread starter Simon Jefferies
  • Start date
P

Peter Huang

Hi Simon,

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you wants to copy file to the
windows explorer by drop an item(usually the filepath string) from listview
to window explorer.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

I think we can use the FileDrop DataFormat to do the job.

<Code Snippet>
Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
ListView1.AllowDrop = True
ListView1.View = View.Details
ListView1.Columns.Add("Column1", ListView1.Width - 4,
HorizontalAlignment.Left)
ListView1.Items.Add("C:\test.txt")
End Sub

Private Sub ListView1_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter
If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
e.Effect = DragDropEffects.All
Else
e.Effect = DragDropEffects.None
End If
End Sub

Private Sub ListView1_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop
Dim s() As String = e.Data.GetData("FileDrop", False)
Dim i As Integer
For i = 0 To s.Length - 1
ListView1.Items.Add(s(i))
Next i
End Sub

Private Sub ListView1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
If ListView1.SelectedItems.Count > 0 Then
Dim strFilesPath() As String
ReDim strFilesPath(ListView1.SelectedItems.Count - 1)
For i As Integer = 0 To ListView1.SelectedItems.Count - 1
strFilesPath(i) = ListView1.SelectedItems(i).Text
Next
Dim dt As DataObject = New DataObject(DataFormats.FileDrop,
strFilesPath)
ListView1.DoDragDrop(dt, DragDropEffects.Copy)
End If
End Sub
</Code Snippet>

The code above is for demostration purpose, you may need to modify it to
adapt to your senario.
Please apply my suggestion above and let me know if it helps resolve your
problem.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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