In this tutorial we learn how to drag and drop images and text files from desktop to our application, and how to drag and drop controls inside Window in REALbasic. Watch it, learn it, enjoy it!
We will use today a StaticText, TextArea and Canvas control to create our GUI. Simply drag and drop this controls to Window1 so they will be placed to something similar to the picture below.

The firs thing we will do in this tutorial is dragging a StaticText control to TextArea control and displaying in it StaticText1.Text. To do this go to StaticText1 Function MouseDown () and use this code.
1 2 3 4 5 | Dim d As DragItem d = NewDragItem(me.left,me.top,me.width,me.height) d.text = me.text d.drag |
Now you can drag and drop StaticText to TextArea control. Isn’t this easy or what?
In this part of tutorial we will learn how can we drag and drop text file from desktop to our REALbasic application. In this case we will use TextArea control as a object into we will drag and drop text file and display all content in it available. Firs go to Project tab and add new File type set and as Display Name type special/any.

Next go to code editor and in TextArea1 Sub Open () enter code… Of course you can be more detailed by allowing only a specify format for drag and drop in this control.
1 | me.AcceptFileDrop("special/any") |
We will create a Drag and drop event in TextArea1 Sub DropObject ()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | Dim text As TextInputStream Dim f As FolderItem do if obj.FolderItemAvailable then f = obj.FolderItem if f < > Nil then // f various Nil text = f.OpenAsTextFile TextArea1.Text = text.ReadAll text.close else //User Cancelled end if end if loop until not obj.NextItem |
As you can imagine if you will now drag and drop a text file to this control it will read and display the text inside the text file.
Getting a picture or graphic from desktop (or should say from hard drive) into Canvas using drag and drop method is similar to previous paragraph. Go to code editor and in Canvas1 Sub Open () enter code:
1 | me.AcceptFileDrop("special/any") |
Create a Drag and drop event in Canvas1 Sub DropObject ()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Dim f As FolderItem Dim p As Picture do if obj.FolderItemAvailable then f = obj.FolderItem if f < > Nil then // f various Nil p = f.OpenAsPicture // load picture to memory Canvas1.Backdrop = p // load image from memory to canvas else //User Cancelled end if end if loop until not obj.NextItem |
If you like you can add to this project some image manipulation techniques like fit to canvas, crop etc. from tutorial Creating ImageViewer in REALbasic. See you next time!
tks. it has been very usefull.
good tutorial
Cool, I was looking for this solution!