Drag & Drop 101
Picking up the Dragon*
Tim Converse
dBASE Inc.
09/12/00

a primer on using drag and drop in dBASE


Introduction

dB2K introduced many new features to the dBASE developer.  One of the best is Drag & Drop functionality.  Drag & Drop is used in many programs currently used today and in Windows itself.

Now you can add the same functionality to your dBASE applications.

Let's take a quick look at the basics of Drag & Drop.

Drag & Drop is the ability to pick up and move objects or information in your application. To see a simple example of this open the Windows Notepad and the Windows Explorer.  Find a text file in the Windows Explorer.  Click and hold the left mouse button down on the text file, then move the mouse pointer to Notepad and let up on the mouse button.  If you did everything correctly Notepad will open up the text file you dragged into it.


Example 1

Now let's create a simple dBL example.

               this.drag(this.name,"O","") If you've done everything right, the EntryField should now be on top of the container.  If it isn't, put the form in design mode, highlight the container and use the LAYOUT | SEND TO BACK menu choice, then rerun the form.



Explanation of Example 1

So, let's look at our simple example.

On the Container, we set it's allowDrop property to True.  This tells the container that if an object is dragged to it, do the following things:

 (Note: This is true only on when the dragEffect of the object is set to 2-Copy.  1-Move will not fire the onDrop event.)

On the EntryField, we set the dragEffect property to 2-Move.  This tells the EntryField that if a drag operation is initiated, do the following things:

But what is this drag operation that is being referred to?

That's the line of code in the onLeftMouseDown event of the EntryField.

 this.drag(this.name,"O","")
drag is the method which initiates all Drag & Drop events.  Once a drag operation begins, all other mouse events are superseded.  The three parameters passed in this case are the name of the object which has called the drag method, the letter "O" and nothing.

These parameters are referred to as:

In this case, the cName parameter is the name of the object itself.

The cType parameter of "O" is a bit more mysterious.  Although the parameter can be any string you choose up to 260 characters long, there are a couple of strings which have some meaning to dBASE internally.

Finally, the empty parameter at the end is simply the unimplemented icon parameter.  Although it currently is not used, the call to the drag method does require that it be included.


Definitions

There are two basic catagories of Drag & Drop objects.  Drop Sources and Drop Targets.  Some objects are both a Drop Source and a Drop Target.

Drop Sources are objects that initiate a Drag & Drop operation.  These are the objects which can be 'dragged'.

Drop Targets are objects which accept a Drag & Drop operation.  These are the objects which can have other objects 'dropped on them'.

Objects which can be Drop Sources all have:

Objects which can be Drop Targets all have:
allowDrop
Defaults to False.  When set to False, objects which are being dragged will not be allowed to drop on them.  The drag Icon will not show any change indicating that this is a possible place to drop an object.

 When set to True, the mouse icon will change indicating that it is possible to drop on this object.

dragEffect
 Defaults to 0-None.  It can be set to 1-Copy or 2-Move.
onDragBegin
This event is fired when a drag event has been initiated.
onDragEnter
This event is fired when the mouse pointer enters a possible drop target during a drag event.
onDragLeave
This event is fired when the mouse pointer leaves a possible drop target during a drag event.
onDragOver
This event is fired when the mouse pointer is over a possible drop target during a drag event.
onDrop
This event is fired when a drag event is ended on a possible drop target.
drag
This method is the heart of Drag & Drop.  It is called to begin a Drag & Drop operation.  It takes the following three parameters: The drag event is usually initiated inside the onLeftMouseDown event of a Drop Source, though it can be initiated anywhere you choose.

 The parameters are not enforced in dBASE.  They can contain any information you need to accomplish the operation you are attempting.



Example 2

Now let's try another example.

In this example we will use Drag & Drop to move items from one Listbox to another.

What did we do here?

In the onOpen of the second Listbox we created a new property of the Listbox called sourcearray and then set it's datasource to that array.

In the onLeftMouseDown event of the first Listbox we initiated a drag operation, passing the parameters of the name and current value of the first Listbox.

In the onDrop of the second Listbox we took the value being carried in the drag operation and added it to the sourcearray property of the second Listbox and then refreshed the datasource of the second Listbox with that sourcearray.



List of Drag & Drop Objects

So what objects have Drag & Drop capabilities?

The following objects all can be used as Drop Sources:

 ActiveX, Browse, CheckBox, ComboBox, Editor, EntryField, Grid,
 HScrollBar, VScrollBar, Image, NoteBook, Ole, PaintBox, Progress,
 PushButton, RadioButton, ReportViewer, Slider, SpinBox, TabBox,
 Text, TextLabel, TreeView.
The following objects all can be used as Drop Targets:
 Container, Browse, Listbox, Image, Grid, Notebook, Paintbox,
 ReportViewer, TreeView.


Summary

What can you do with Drag & Drop?  Just about anything you want really.  You will find that it's very flexible and will allow you to do many things.  For a really superior example of just how much you can do with Drag & Drop, look at dQuery.

dQuery takes full advantage of the possibilities of Drag & Drop capability and may inspire you to create your own Drag & Drop capable applications.



Copyright 2000, Timothy E. Converse.
All rights reserved.

Information about dBASE, Inc. can be found at:
       http://www.dbase.com



* The hard part isn't the drop.  It's picking up the dragon in the first place.