Link to home
Start Free TrialLog in
Avatar of dthillsr
dthillsr

asked on

Drag and Drop From ListView to ListBox

I am using TreeView to display files in ListView  which works fine. I am trying to drag a file from the ListView to a ListBox to do things with the file but I cannot make it work. I do have Allow Drop set to True in the ListBox. Below is the code I have so far.

For the ListView
Private Sub lstDirectoryInfo_ItemDrag(sender As Object, e As ItemDragEventArgs) Handles lstDirectoryInfo.ItemDrag
        Dim lvItem As ListViewItem
        lvItem = CType(e.Item, ListViewItem)
        Dim importantData As String
        importantData = lvItem.Text
        Dim drp As DragDropEffects
        drp = lstDirectoryInfo.DoDragDrop(e.Item, DragDropEffects.Copy)
        MessageBox.Show(drp.ToString())
    End Sub

For The ListBox
Private Sub lstSFTP_DragDrop(sender As Object, e As DragEventArgs) Handles lstSFTP.DragDrop
        Dim str As String = CStr(e.Data.GetData(DataFormats.StringFormat))
        lstSFTP.Items.Add(str)
    End Sub
    Private Sub lstSFTP_DragEnter(sender As Object, e As DragEventArgs) Handles lstSFTP.DragEnter
        If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub

What I want to accomplish is have the filename in the ListBox.

Thank You
Avatar of ktaczala
ktaczala
Flag of United States of America image

looks like you assign drp to the dodragdrop function but you never enable the dodragdrop function for the listview
drp = lstDirectoryInfo.DoDragDrop(e.Item, DragDropEffects.Copy)
try this:
lstDirectoryInfo.DoDragDrop(e.Item, DragDropEffects.Copy)
Avatar of dthillsr
dthillsr

ASKER

That did not help. I need to mention also that when I click on an item in the listbox I get a circle with a line through it for the cursor I have enclosed a document with a print screen of that.
Drag-Error.docx
I think the issue is in your dragenter event. You are only allowing a drop if the item being dragged is a file which it isn't.
Would you know how I would adjust the code?
I have figured this out and no longer need assistance.
I've requested that this question be closed as follows:

Accepted answer: 0 points for dthillsr's comment #a40687755

for the following reason:

I did more research on listview in order to correct the problem.
and your solution was?  please do not enclose graphics in a word document in the future
ASKER CERTIFIED SOLUTION
Avatar of dthillsr
dthillsr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial