Resolved Drag and Drop Out to another program

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
491
Programming Experience
3-5
How can i drag a filename from a listviewitem out from my program to another program that is capable from receiving file drop from windows explorer?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,493
Location
Sydney, Australia
Programming Experience
10+
As long as the other application is written to support standard drag and drop operations, it's no different to doing it within your own application, except you only have to do part of the work. I would suggest starting here. That is specifically about dragging into your app from another but it also says this at the bottom:
Additionally, you can configure your TextBox control to allow text strings to be dragged and dropped into WordPad. For more information, see Walkthrough: Performing a Drag-and-Drop Operation in Windows Forms.
and provides that link that you should follow. I would suggest that you work through that to understand the principles involved first. I would then try coding both the drag and drop operations using the file path in your own application to make sure that the drop end is getting the correct data. You should then be able to just do the drag part in your app and the drop part will work in File Explorer.
 

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
491
Programming Experience
3-5
my app has the drop function and i can drag files from the desktop to the app just fine. But how do you drag from a listview into another app? Do i need to have a function for dragging out from the listview?
When i do a mousedown event on the listview and a DoDragDrop, I can drag the filename to a text editor just fine, but it doesnt do anything to the app i wan to drag into. Is it not the file name i need to drag into the other app? It accept windows explorer file drag
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,493
Location
Sydney, Australia
Programming Experience
10+
My guess (it has to be a guess, because you haven't actually provided all the relevant information about what you're doing) is that you're using the wrong data format. I suspect that you are using Text or the like when you probably need to use FileDrop. I would suggest that you do the same drag and drop into your app from File Explorer that you do to the other app and see what data formats are available and then make sure that those same formats are available when you drag from your app.
 

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
491
Programming Experience
3-5
I know you are right but how do i convert this filename into a filedrop when i dodragdrop? Is it just a ctype? Also isnt filedrop a string?
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,493
Location
Sydney, Australia
Programming Experience
10+
I just dragged a file from my application to File Explorer using this code:
VB.NET:
Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
    Dim data As New DataObject()
    Dim filePaths As New StringCollection

    filePaths.Add(Path.Combine(Application.StartupPath, "TestData.txt"))
    data.SetFileDropList(filePaths)
    DoDragDrop(data, DragDropEffects.Copy)
End Sub
If you are doing drag and drop just within your application then you can use whatever you want as that first argument to DoDragDrop and look for that same type when dropping. If you're doing it between applications then you do as I have here, i.e. create a DataObject, add data to it in the appropriate format(s) and then pass that to DoDragDrop. That will ensure that any application that supports drag and drop between applications will be able to access that data in the appropriate standard format.

That DataObject class also has SetText, SetImage and SetAudio methods for specific data types and a SetData method for everything else. They are the complementary methods to the e.Data.GetX methods you call in a DragDrop event handler.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,493
Location
Sydney, Australia
Programming Experience
10+
Without having tested further, I think that it may be the case that if you pass anything other than a DataObject into DoDragDrop (probably it would be more accurate to say anything that doesn't implement IDataObject) then it will be wrapped in a DataObject anyway, so you will always get that back when you drop. If you were to just pass in a String then I would guess that that would be the same as doing what I did above but calling SetText and passing that String instead of calling SetFileDropList. If you don't specify the FileDrop data format then the receiving application will just treat it as any old text rather than the path of a file/folder, so it won't consider it to represent a file to copy/move.
 
Top Bottom