VB.NET - Drag and Drop into listview - Asked By jenn on 03-Jun-10 02:08 PM

man o man I am beginning to wonder when Egghead is gonna start charging me rent here!! lol..

okay, as some of know I am now working on a "Listview" and I am not so versed in this tool, as it is the first time I have tried to use it (for that matter, I am not so very well versed in the whole visual studio program!)

anyway..
here is what I am trying to do and my questions:
I have my listview set up on details, I have added the ImageList tool to my form..enabling me to resize my row height. And I have it all right how I want it. :)

thanxXx again for the people here that helped me accomplish this!

Now what I want to do is this:
I have a folder on my hard drive with 5 small images in it. I want to place the folder, or a way for my user to select any OR all of the 5 images..and drag n drop them into a block on the listview.

ONE...I do not know anything about this drag n drop feature, other than you have to set the drag/drop feature to true in the property box for listview.

TWO...how do I allow them access to these 5 images, without making my images disappear??? like I said ...I dont know a thing about this LOL

For example: if the user does drag and drop one of my images ..will that action cause my image to no longer be available for future use? So, in other words..on wednesday..mark decided to drag and drop Image1 into the listview..and at the end of the day...he saves and closes the program. When he comes in tomorrow, and boots into my program..will the image still be available for him to choose again??

OR..do I somehow have to use a COPY..PASTE..for this instead of Drag n Drop..

ooOOoo..okay now that I have evryone CoNFuSeD...(lol) I am gonna go and grab a cup o coffee, as I feel im gonna need it!

thanx all :)
Devil Scorpio replied to jenn on 03-Jun-10 02:34 PM
Hi Jenn,

The first step is to enable dragdrop on the controls.
In the properties for all controls that you want to allow
dropping into, set "AllowDrop" property to "True".

In this example I have two ListViews that you populate
with files and folders from a location
on your computer.

We will be able:
1. drag from one ListView to the other
2. drag into windows explorer
3. drag from windows explorer into our application.

In the example I have placed two class scope variables
at the top.
The first thing to remember and account for:
To move or copy files or folders by dragdrop, you need paths;
File.Move(oldPath, newPath)
Directory.Copy(oldPath, NewPath)
So the "class variables", as I will refer to them (since
there are only two), will hold the path information for the
ListViews.

First the drag:
Code:

Private Sub OnMouseDrag(ByVal sender As Object, _
ByVal m As System.Windows.Forms.ItemDragEventArgs) _
Handles ListView1.ItemDrag, ListView2.ItemDrag

       
'The strings should explain themselves, they will be
        '
used to recreate full file and folder paths
       
Dim strPath, strName, strFullPath As String

       
'Determine which ListView is being Dragged From
        '
so we can get the correct parent path from the
       
'class variable
        Dim LView As ListView = sender
        If LView Is ListView1 Then
                strPath = LView1Path
        ElseIf LView Is ListView2 Then
                strPath = LView2Path
        Else
                Exit Sub
        End If

        '
Put the dragged ListView Item or Items into a variable
       
Dim items As ListView.SelectedListViewItemCollection = _
       
LView.SelectedItems

       
'Create a string list of File and Folder Paths
        Dim DropList As New StringCollection

        '
Iterate through the dragged (selected) items
       
'Since the item is selected and a item is being
        '
dragged....which triggered this event, we will
       
'assumed all selected items want to be dragged

        For Each item As ListViewItem In items
                strName = item.Text
                strFullPath = strPath & "\" & strName

                '
...."FileInfo".... Another critical type

               
Dim FtoDrop As FileInfo = New FileInfo(strFullPath)

                 
DropList...StringCollection

               
DropList.Add(strFullPath)
       
Next

       
' The "DataObject"...critical to the operation
        Dim DragPaths As New DataObject()

        '
Now we use the ".SetFileDropList()" Method to set the
       
'File drop list into the "DataObject"
        DragPaths.SetFileDropList(DropList)

        '
The standard dragdrop method. If you do not need to
       
'limit dragdrop effects for a reason, then set to "All"

        DoDragDrop(DragPaths, DragDropEffects.All)


End Sub

The Bulk of the code is dealing with the path strings.
This is where major differences will occur in other applications
since there are so many ways of handling and representing files.
But before we get to the dragdrop stuff....we just have a path to
a file or directory.

Then we put all of the paths into an array, collection, arraylist
whatever container you would prefer. Must be "String" data.

Put the paths into a "DataObject". DragPaths is our DataObject
DragPaths.SetFileDropList(DropList)

Then finally:
DoDragDrop(DragPaths, DragDropEffects.All)

DragDropEffects.All will allow the control recieving the
dragged items to do whatever it wants with them...Move, copy,
link.


So that that part is easy huh?
Guess what???? Now, with this code alone (dragdrop code)
you can drag an file or folder from your application into
Windows Explorer!!
(remember, the ListViews do not update themselves, so you will
not be able to see the change in the ListView.)
Go to the folder in Windows and watch the file move when you
move it out of your application.

So now, we want to recieve the dropped file or folder.

Code :
Private Sub LView_DragEnter(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DragEventArgs) _
Handles ListView1.DragOver, ListView2.DragOver

       
'When you drag over a control, it evaluates the object
        '
being dragged to see if it can accept it.
       
'Here, the data must be in data format "FileDrop"
        If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then

                '
If "move" is allowed by the origin, then the file
               
'will be moved. We set our ListViews to "All".
                '
So move is allowed.
               
'At this point, more conditions can tested to
                '
determine what action to take i.e. move, copy...
                e
.Effect = DragDropEffects.Move
       
Else
                e
.Effect = DragDropEffects.None
       
End If

End Sub

Now, our ListView is ready to recieve a drop event:
Code :
Private Sub LView_ItemDropped(ByVal sender As Object, _
       
ByVal e As System.Windows.Forms.DragEventArgs) _
       
Handles ListView1.DragDrop, ListView2.DragDrop

   
'Now add the final routine...the dragdrop event
   '
To copy or move a file, we need two Paths:
   
'The current path and destination path.
        '
The current path is obtained from the file to be dropped
       
'The destination path is obtained from the class
        '
  variables set up for the ListViews

       
'This extracts the collection of path strings from the
        '
DataObject
       
Dim Paths As String() = _
       
DirectCast(e.Data.GetData(DataFormats.FileDrop), _
       
String())

       
'To get the destination path, find out which ListView
        '
called this Event, then use the appropiate class variable
       
Dim strDestination As String
       
Dim LView As ListView = sender
       
If LView Is ListView1 Then
                strDestination
= LView1Path
       
ElseIf LView Is ListView2 Then
                strDestination
= LView2Path
       
Else
               
Exit Sub
       
End If

       
'iterate through the path strings
        For Each path As String In Paths

                '
The following "If" block will determine if the
               
'path is a file or directory
                If File.Exists(path) Then

                        '
This same method was used to populate
                       
'the ListView
                        Dim _file As FileInfo = New FileInfo(path)
                        Dim _fileName As String = _file.Name

                        '
Create a path from the peices
                       
Dim newPath As String = _
                                strDestination
& "\" & _fileName
                        Try

                                'Move the file!!
                                File.Move(path, newPath)

                        Catch ex As Exception
                                ' This is not about error handlers so we
                                ' will just ignore the error and keep on
                                ' going
                                Continue For
                        End Try
                        'If it isn't a file, then it should be a
                        'directory
                ElseIf Directory.Exists(path) Then

                        Dim dir As DirectoryInfo = _
                                New DirectoryInfo(path)
                        Dim dirName As String = dir.Name
                        Dim newPath As String = _
                                strDestination & "
\" & dirName

                        Try
                                Directory.Move(path, newPath)
                        Catch ex As Exception
                                Continue For
                        End Try
                End If

        Next
End Sub
Remember what I said earlier...it's all about paths...
We need to get some paths!!

The container we put the paths into must be able to hold an
undetermined number of items...even if we know for sure only
one item is being dropped.

Dim Paths As String() = _
DirectCast(e.Data.GetData(DataFormats.FileDrop), String())

We now have the paths!!!....That's it!!!
The rest of the code is determining what to do with them.
We put them into a collection....now take them out of
a collection, manipulate the string data to create the needed
paths, every thing we have already done...but in reverse.

Then the final Action...move, copy, link.....

Download Drag & Drop Example

Download

Regards :)
jenn replied to Devil Scorpio on 03-Jun-10 03:56 PM
WOW..LOL

did you take a breath somewhere in all of that?????? Holyyyyyyy Toledos!! hehehehee

okay, first off: how come the 2 listviews?? I only need one hehee( I am using the same one over and over) closing it at end of day..and when it opens again, it is blank (the contents) are but the form is still designed and ready to use.

So i dont understand the 2 listviews.

and, all I really wanna do is give the user access to a folder. If possible, even place the folder itself right on the form so that they can open it up...and click on a image and place it where ever they want to on this listview/block grid I got happening here LOL!!

I just dont know how place a folder onto a form,, or even if it is possible..
and if it is, how to do it!

so what this is all seeming to boil down to is:
i got a huge grid: 4 columns wide and 8 rows deep. and the images that I have to go into these blocks are much smaller than the blocks because I need them to be able to drag n drop maybe 3 of the images into 1 block..if not all 5 of the images into 1 block (dont even know if that is possible either! lol) but its the plan..

I did some testing and did find out that if you simply copy and paste one of the images, well it defaults to the first column :( so the copy n paste method wont work)

however if I can drag an drop where I want it, then I can have 1-5 of these small images in 1 block at one time :)

my question to this is "is the drag n drop function..going to allow me to retain the image in the folder so that it can be used again in another block if we need it to be??

or am I barking up the wrong tree here..