Resolved How to copy/move files dropped onto a control?

.Ghost.

New member
Joined
Jul 8, 2008
Messages
2
Programming Experience
1-3
Hello, I am new here, and I apologize firstly if this is in the wrong section or topic, but this seemed like the correct place.

I have a windows form, with basically four "instances" of a Control, two buttons and a picture box. The picture box shows the icon of a folder chosen by the user, the buttons open the folder in windows explorer and allow the user to choose a new folder.

The control. I have attempted this with a panel, picture box and a listview, all with AllowDrop set to true.

ListView1 = ListView control
RemovePathSizeGet....RemovePath(Path As String) returns the name of the file from the path, by seperateing the path by "\" and returning the last part (Name).
Box2Path = String containing full path of a folder not ending in "\"

My code for the drop events are below:
VB.NET:
    Private Sub ListView1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop
        Try
            'Retrieve the data in the array format.
            'Add the dragged items to the PATH
            Dim FileToCopy As String
            Dim NewCopy As String
            Dim FileName As String

            FileToCopy = e.Data.GetData(DataFormats.FileDrop)
            FileName = RemovePathGetSizeTwo.PathRemove.RemovePath(FileToCopy)
            NewCopy = Box2Path + FileName

            If System.IO.File.Exists(FileToCopy) = True Then
                System.IO.File.Copy(FileToCopy, NewCopy)
                MsgBox("File Copied")
            End If
        Catch
            MsgBox("Could not copy the selected files. See help.")
        End Try
    End Sub

    Private Sub ListView1_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter
        Try
            'Check for the DataFormat 'FileDrop'
            If e.Data.GetDataPresent(DataFormats.FileDrop) Then
                'set the Effect of drag-and-drop operation to Copy (adds a '+')
                e.Effect = DragDropEffects.Copy
            Else
                'Else set the Effect of drag-and-drop operation to None
                e.Effect = DragDropEffects.None
            End If
        Catch
            MsgBox("That isn't a valid file.")
        End Try
    End Sub

I have probably made a fairly obvious mistake, but it escapes me.

When I drop a file onto it, from any location, it simply shows the message box "Could not copy the selected files. See help."

Any help is very much appreciated ^^ And sorry this is my first post, but it's the first time I've not been able to find an answer by searching through google and the websites it throws up.
 
Last edited:
First off, if you show the error message it'll tell you what's wrong:
VB.NET:
        Catch Ex As Exception
            MessageBox.Show("Could not copy the selected files. See help." & Environment.NewLine & Ex.Message)
        End Try
    End Sub

Second of all, here's how I get the file path(s) of the files being dropped:
VB.NET:
    Private Sub ListView1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop
        Try
            Dim strFileNames() As String = GetDroppedFilePath(sender, e)
            For Counter As Integer = 0 To strFileNames.GetUpperBound(0)
                If strFileNames(Counter) <> String.Empty Then
                    'Handle this file here
                End If
            Next Counter
        Catch Ex As Exception
            Messagebox.Show(Ex.Message)
        End Try
    End Sub

#Region " GetDroppedFilePath "
    <System.Diagnostics.DebuggerStepThrough()> Private Function GetDroppedFilePath(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) As String()
        Dim strNames() As String, intCounter As Integer = 0I
        Dim strMyFileNames(intCounter) As String
        Try
            'a file dropped from Explorer:
            If e.Data.GetDataPresent(DataFormats.FileDrop, False) = True Then
                strNames = CType(e.Data.GetData(DataFormats.FileDrop), String())
                For Each strName As String In strNames
                        ReDim Preserve strMyFileNames(intCounter)
                        strMyFileNames(intCounter) = strName
                    intCounter += 1
                Next strName
            End If
            Return strMyFileNames
        Catch ex As Exception
            Return strMyFileNames
        End Try
    End Function
#End Region
 
I used this code except it doesn't allow me to drop anything onto the listview control. I just get the cross-through-circle mouse pointer.

It goes straight past the:

If strFileNames(Counter) <> String.Empty Then
'Handle this file here
End If

section. It get's to the "If" and then goes straight to "End If"

I'll try if it does it under differerent conditions (Dragged file location, number of files)

How should I refer to the files in the dragdrop commented area?

Do I use strFileNames(Counter) to refer to the current path? That's wht i am currently doing, though it makes no odds on the if statement.

Oh wait.. I think I've made another "newb" mistake. I have a habiot of making simple ones... I believe in the function I don't want (for my app) the:

If Ends In "xml" Then... statement.. since I want it to accept all files?

SOLVED

Yes that was the case, I knew it'd be something like that. I really need to read things slower when I check them >.>"

Thankyou um... the poster person, I can't see your name from the reply page :(

Ah, Thankyou JuggaloBrotha, you're a life saver ^^
 
Last edited:
Yes, I had neglected to take that xml check out, the project that I copied the code from can only accept xml files. Sorry about that, I've edited my previous post and have taken the xml check out.

Glad to hear you've got things working.
 
Back
Top