Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want replicate the drag & drop from file explorer but from my application which will be lislview item.

i.e.
Drag listview item (File path) to other application the same as explorer would work.

There seems to be a lot of example dragging to an application but very few to another.

Thanks in advance

What I have tried:

I have tried this in mouse down but not the correct approach
DataObject Data = New DataObject(DataFormats.FileDrop, files)
DragDrop.DoDragDrop(listTest, Data, DragDropEffects.Copy)


I have even thought of going for clipboard but again I don't think the is the correct appoach.


Sorted (Just if anyone else is struggling.

<ListView x:Name="Lst_File_Viewer" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Margin="5,3,5,3" SelectionMode="Single">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <EventSetter Event="MouseMove"  Handler="Lst_File_Viewer_MouseMove" />
        </Style>
        
    </ListView.ItemContainerStyle>

    <ListView.View>
        <GridView AllowsColumnReorder="False">
            <GridViewColumn Header="File Name" Width="Auto" DisplayMemberBinding="{Binding Name}" />
            <GridViewColumn Header="Dir" Width="0" DisplayMemberBinding="{Binding Directory}" />
        </GridView>
    </ListView.View>
</ListView>


Private Sub Lst_File_Viewer_MouseMove(sender As Object, e As MouseEventArgs)
    If e.LeftButton = MouseButtonState.Pressed Then

        If e.Source IsNot Nothing Then
            Dim dataObject As DataObject ' = New DataObject(myList)
            Dim myList As StringCollection = New StringCollection
            For Each Item As File In Lst_File_Viewer.SelectedItems
                myList.Add(Item.Directory)


            Next


            dataObject = New DataObject()
            dataObject.SetFileDropList(myList)

            DragDrop.DoDragDrop(Lst_File_Viewer, dataObject, DragDropEffects.Copy)
        End If
    End If
End Sub


Thanks for setting my down the correct Path.
Posted
Updated 3-Feb-24 6:06am
v4

1 solution

When you drag'n'drop from explorer into an app, the receiver neds to do a couple of things:
1) Handle the DragEnter Event Set the e.Effect to DragDropEffect.Copy, or DragDropEffects.Move
2) Handle the DragDrop Event and fetch the list of files by calling e.Data.GetData and passing it the DataFormts.FileDrop value. This returns an Object which if there are files will be an array of strings - one string per file and containing the full path to the file.

So to drag out of your app in a format the other system can process is pretty simple: build an array of paths and call the DoDragDrop method for your Form (or another control) passing the array and DragDropEffects.Copy Ored with DragDropEffects.Move as the second parameter.

You may also want to look at the Control.QueryContinueDrag Event (System.Windows.Forms) | Microsoft Learn[^] but if you are dropping on "any application" then you shouldn't need it.
 
Share this answer
 
Comments
Knight school 3-Feb-24 10:16am    
Thanks, I think the issues are linked to the Listview item. So I have got to this which is giving me a cursor change (I think this would be pointing to Drag started but I don't seem ti get to the drop.
Here is where I'm at:-
<listview x:name="Lst_File_Viewer" grid.row="2" grid.column="0" grid.columnspan="2" margin="5,3,5,3" selectionmode="Single">
<listview.itemcontainerstyle>

<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<EventSetter Event="MouseMove" Handler="Lst_File_Viewer_MouseMove" />
<EventSetter Event="Drop" Handler="Lst_File_Viewer_Drop"/>




<listview.view>
<gridview allowscolumnreorder="False">
<gridviewcolumn header="File Name" width="Auto" displaymemberbinding="{Binding Name}">
<gridviewcolumn header="Dir" width="0" displaymemberbinding="{Binding Directory}">







Private Sub Lst_File_Viewer_Drop(sender As Object, e As DragEventArgs)

MessageBox.Show("Drop Happened")
'Not firing at all

End Sub

Private Sub Lst_File_Viewer_MouseMove(sender As Object, e As MouseEventArgs)
If e.LeftButton = MouseButtonState.Pressed Then

If e.Source IsNot Nothing Then
Dim myList As List(Of String) = New List(Of String)
For Each Item As File In Lst_File_Viewer.SelectedItems
myList.Add(Item.Directory)
Next

Dim dataObject As DataObject = New DataObject(myList)
DragDrop.DoDragDrop(Lst_File_Viewer, dataObject, DragDropEffects.Copy)
End If
End If
End Sub

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900