Drag and Drop to move file

daveofgv

Well-known member
Joined
Sep 17, 2008
Messages
218
Location
Dallas, TX
Programming Experience
1-3
Hello All -

I would like to ask for a little guidence if possible.

I am trying to drag a file from my desktop to my vb.net (2008) form and have the file automatically move to a destination folder that I will create.

I understand the concept of moving, copying and deleting files, however, does anyone know how I can drag a file from the desktop to a form (textbox, panel etc...) and have the file automatically move to another folder? Also, I would like to have the filename that was dragged onto the form show in a listbox so I can see which filename I just moved.

Hope this makes sense.

Thank you in advanced

daveofgv
 
The file will not be automatically moved. The code to move the file will be exactly the same as it would any other time. The difference with using drag and drop is how you get the path of the file that is to be moved.

Drag & Drop in Windows Forms
 
One more question (which just came up).... I did create a drag and drop - on my work laptop (windows 7), however, when I got home I copied it over to my home computer (Vista) and it does not work. I even created aother solution and still will not let the file drag onto the listview.

Am I missing something?

Thanks
 
Am I missing something?
Apparently you are or the code would work, but I have no idea what it would be because you haven't provided any information about exactly what you did. The first thing to do would be to test that the events are being raised when you expect them to be. If you find one that isn't, that is at least part of your issue and you can then investigate why, e.g. a missing Handles clause.
 
Thank you for the reply..... Well, same code from home computer onto my work laptop works, but solution on work laptop does not work on home computer. I suppose there is a setting somewhere on my Vista machine that is not allowing the drag and drop.....

I will start disecting my machine.

Thanks for the reply
 
For my above problem about not working on Vista - not sure what the deal is, however, it will not work in debug mode, however, on Windows 7 it will - probably something with my VS settings (idk). Here is what I have, but can't seem to find the correct way to move each file in the listview (file path) from the location it's at to another folder:

I didn't write all this code - not sure exactly where I got it, however, the credit goes to someone else.

VB.NET:
Imports System.Runtime.InteropServices 'For APIs
Imports System.IO 'Import System.IO For File Operations 
Imports Microsoft.Win32 'The Registry stuff
Imports System.Windows.Forms.Application
Imports System


Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     
        Me.ListView1.AllowDrop = True
        Me.ListView1.Columns.Add("File name")
        Me.ListView1.SmallImageList = Me.ImageList1
        Me.ListView1.View = View.Details
    End Sub


    Private Sub ListView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop
        'The data can only be dropped if it is a file list and it can be copied.
        If e.Data.GetDataPresent("FileDrop") AndAlso _
           (e.AllowedEffect And DragDropEffects.Copy) = DragDropEffects.Copy Then
            'Get the data.

            Dim filePaths As String() = DirectCast(e.Data.GetData("FileDrop"), String())

            Dim upperBound As Integer = filePaths.GetUpperBound(0)
            Dim items(upperBound) As ListViewItem
            Dim filePath As String

            'For each file in the list, create an item, complete with icon.

            For index As Integer = 0 To upperBound
                filePath = filePaths(index)

                If Not Me.ImageList1.Images.Keys.Contains(filePath) Then

                    'This is the first time this file has been added so add its icon too.

                    Me.ImageList1.Images.Add(filePath, _
                                             Icon.ExtractAssociatedIcon(filePath))
                End If

                items(index) = New ListViewItem(filePath, filePath)
            Next

            'Add the items to the ListView.

            Me.ListView1.Items.AddRange(items)
        End If

    End Sub

    Private Sub ListView1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter
        If e.Data.GetDataPresent("FileDrop") AndAlso _
       (e.AllowedEffect And DragDropEffects.Copy) = DragDropEffects.Copy Then

            'A file list is being dragged and it can be copied so provide feedback to the user.

            e.Effect = DragDropEffects.Copy

        End If

    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim Desktop As String = My.Computer.FileSystem.SpecialDirectories.Desktop

        If Not My.Computer.FileSystem.DirectoryExists(Desktop & "\Unused Desktop Icon and Documents") Then

            My.Computer.FileSystem.CreateDirectory(Desktop & "\Unused Desktop Icon and Documents")

        End If


        With Me.ListView1
            For i As Integer = 0 To .CheckedItems.Count - 1

                .Items.Remove(.CheckedItems(0))

            Next i
        End With

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
        Dim Desktop As String = My.Computer.FileSystem.SpecialDirectories.Desktop

        If Not My.Computer.FileSystem.DirectoryExists(Desktop & "\backup storage") Then
            My.Computer.FileSystem.CreateDirectory(Desktop & "\backup storage")
        End If


        With Me.ListView1
            For i As Integer = 0 To .CheckedItems.Count - 1

                .Items.Remove(.CheckedItems(0))

            Next i
        End With

    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        For Each lvitem As ListViewItem In Me.ListView1.Items
            If lvitem.Checked = False Then
                lvitem.Checked = True
            Else
                lvitem.Checked = False
            End If
        Next

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    End Sub
End Class

Apparently, I cannot use the simple

VB.NET:
File.Move()

How would I move the file (where the listview shows the file path) to the folder I created in the above code?

Thanks in advanced

daveofgv
 
Back
Top