Desktop Drag and Drop
From Xojo Documentation
Controls support drag & drop between each other and to or from destinations outside of Xojo.
These are the type of drops that are supported:
- Raw Data
To allow one of these types of drops onto a control, in the Open event for the control call the appropriate "Accept" method. The methods are: AcceptFileDrop, AcceptPictureDrop, AcceptRawDataDrop and AcceptTextDrop. When a control is set to accept a drop it will then receive the various drag events: DragEnter, DragExit, DragOver and DropObject. In most cases you are interested in the DropObject event which passes in a DragItem containing the details of the item being dropped.
Text Fields and Text Areas support textual drag and drop automatically.
Drag Text Onto a Label
- Add a Label and a TextField to a Window layout.
- In the Open event handler for the Label, add this code to allow text drags:
- Add the DropObject event handler to the Label and add this code to display the text that was dropped on it:
Now you can run the project, type some text in the text field, select it and then drag and drop it onto the label to see the label's text get replaced with your selected text.
Drag Label Text Onto a Text Field
Suppose you want to do the opposite and allow the user to drag a label and have its text be added to a text field. Starting with the project you created for Drag Text Onto a Label you can add some additional code to allow the label to be dragged.
- Go back to the Label you added above and add the MouseDown and MouseDrag event handlers.
- Add this code to MouseDown:
- Now add this code to MouseDrag which creates a DragItem, populates it with the text from the Label and then starts the drag:
- Add the Open event handler to the Text Field and add this code to tell it to accept text drops:
- Add the DropObject event handler to the text field with this code to get the text from the drop and display it:
Run the project and drag the Label onto the Text Field to see the Label text replace what is in the Text Field.
Drag a File Onto a List Box
To get a little more advanced, suppose you want to allow the user to drag files from the Finder (or File Explorer, etc.) and add the filename to a List Box. Here are the steps.
- Since you are going to accept files the first thing to add is a File Type Set from the Insert menu or toolbar.
- In the File Type Set, select the first toolbar button (Add Common File Type) and from the dropdown choose "special/any".
- Now go to Window1 and add a List Box to it.
- Add the Open event handler to the List Box with this code to tell it to accept file drops for any file:Me.AcceptFileDrop(FileTypes1.Any)
- Lastly, add the DropObject event handler to the List Box with this code to grab the filename from the dropped file and add it as a row to the List Box:
Run the project and drag a file from the Finder (or File Explorer, etc.) onto the List Box where its name should get added to the list.
Drag a Picture Onto a Canvas
A picture can come from a variety of sources. It could actually be a file so accepting a picture would work similarily to accepting a file as shown above. But a picture can also sometimes come directly from another app such as a web browser or drawing app. This is how you can accept picture drags from other apps to display in a Canvas.
- In order to accept drags of files that are pictures you need to create a File Type Set.
- Add two Common File Types, one for image/png and one for image/jpeg.
- Now add a Canvas to Window1.
- In the Open event handler for the Canvas, add this code to accept drops for Pictures and the PNG and JPG files that you added to the File Type Set:
- Add a property to the window which will store the picture that gets dropped onto the window so that the Canvas can draw it:DropPic As Picture
- Add the Paint even to the Canvas. This code draws the picture when it is available:
- Add the DropObject event to the Canvas with this code which determines the type of drop, gets the Picture and then tells the Canvas to draw it:
You can now run the project and drag a picture file onto the Canvas area. Some browsers (such as Safari) also let you drag a picture from the browser onto the Canvas area.
Drag Rows From a List Box to a Text Field
You can drag rows within a single List Box for reordering by turning on the EnableDragReorder property. But if you want to allow individual rows to be dragged out of a List Box you have to do a bit more coding. For this example you will allow a List Box row to be dragged onto a TextField.
- Add a List Box to a Window and turn its EnableDrag property to ON from the Inspector.
- Add some initial data to the List Box using the InitialValue property in the Inspector.
- Add the DragRow event handler to the List Box with this code to make a DragItem using the text from the row:
- Now add a TextField and in its Open event handler tell it to accept text drops:
- Add the DropObject event handler to the TextField with this code to display the text from the drop:
Now you can run the project and drag a row from the List Box onto the Text Field where the text will be displayed.