Search code examples
realbasicxojo

How do I drag and drop an external file to my xojo app and return the path


I wanted to drag a file (say abc.txt) to my xojo program and let it to write out the path of the dropped file, returning something like C:\\mydata\abc.txt.

How do I go about doing it? Do I need to enable some properties?

I can't find anything useful from manual or forum.


Solution

  • First, add a File Type Set to your project. It'll be named FileTypes1 initially, but better rename it to "DropTypes". Add file types to it that you like to accept. To accept any file, click on the center of these buttons in the IDE's File Type Set editor:

    File Types Set options

    Choose special/any.

    Next, add this line to the Open event of the control or window that should allow drops:

    me.AcceptFileDrop DropTypes.All
    

    Then add this code to the control's or window's DropObject event:

    if obj.FolderItemAvailable then
      dim f as FolderItem = obj.FolderItem
      ' Now you have the file reference in f.
    
      ' Get the path:
      dim path as String = f.NativePath ' (in older RB versions, use *f.AbsolutePath* instead)
    
      ' Show the path:
      MsgBox path
    end