Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

GetFormat, SetFormat

These are methods of the data object that contains data being dragged with OLE Drag and Drop. GetFormat tells you what kinds of data are available. SetFormat lets you expand that list.

Usage

lIsItThere = oDataObject.GetFormat( nFormat | cFormat )
oDataObject.SetFormat( nFormat | cFormat )

Parameter

Value

Meaning

lIsItThere

 

.T.

The data object contains data in the specified format.

.F.

The data object doesn't contain any data in the specified format.

nFormat

1, 7 or 13

The data object contains textual data in ASCII, OEM or Unicode format, respectively. As far as we can tell, no VFP controls automatically supply either OEM or Unicode format data (using the American English version of VFP and Windows), though other applications may and you can provide data in one of those formats.

15

The data object contains a list of files.

Any other number

The data object contains data in a custom format.

cFormat

"OLE Variant Array"

The data object contains multiple data items, in their original format. We haven't been able to find any drag source that automatically provides data in this format, but it can be used to drag the contents of an array without converting all the data into character.

"OLE Variant"

The data object contains data in its original type. For example, data from a spinner is returned as numeric, using this format, while it's character with format 1.

"VFP Source Object"

The data object contains a reference to the data source for this drag. This is always true when dragging from VFP objects.

Any other string

The data object contains data in a custom format.

These methods are a little tricky. First, they belong to an object that isn’t a VFP object. The data object is a COM object created when an OLE Drag and Drop operation begins, in order to hold the data being dragged. It’s capable of holding many different kinds of data, and even of holding the same data in more than one form.

GetFormat lets you find out what kinds of data it’s holding at the moment. SetFormat lets you tell the data object that it’s capable of holding data in a specified format. Both methods are only relevant in the OLE Drag and Drop methods, because those are the only ones where a data object exists. (Actually, if the OLE Drag and Drop methods call other methods and pass the data object as a parameter, you can call its methods from there, too.)

For reasons known only to the folks who designed this stuff, formats can be either numbered or named. So both methods accept either a number or a string for the format parameter.

There are a handful of built-in formats, including one that seems to have been created just for VFP. In addition, you can create your own formats and stuff whatever data you want into the data object for that format. This lets you do very cool things, like dragging a picture onto a form and turning it into wallpaper, or one we saw demonstrated at DevCon, dragging data from VFP and turning it into a table in Word.

To create a format, you call SetFormat in the drag source’s OLEStartDrag method and pass the name for your format. (You can pass a number, but we don’t recommend it. It’s a lot easier to remember “Tamar’s Special Format” than 37.) Once you’ve created a format, you can stick data for your format into the data object using its SetData method. You can add the data in OLEStartDrag or wait until drop time, and call SetData from the source’s OLESetData method.

You don't actually have to explicitly create new formats with SetFormat. You can just call SetData, passing both the data and the format.

Back to more regular operations. When a drop takes place, you can check what formats the drag source contains and then figure out what to do. For example (in fact, the example below), if data is dragged from a listbox into an edit box, you might choose to put a comma-delimited list of all the selected items into the edit box.

Any given drag source may support more than one format. In fact, most of the VFP controls do so. That’s because the “VFP Source Object” format was specially created to give you access to the actual drag source, so all VFP controls support that one. In addition, many of them also support the text (1) and “OLE Variant” formats.

When data is available in multiple formats, you have to figure out which one to use at drop time. However, VFP’s default behavior in this area seems logical to us. For the most part, the choices it makes are the ones we’d normally want. We can write code for the rest.

Example

* This code could go in an edit box's OLEDragDrop method.
* If the data dropped comes from a listbox, it creates a
* comma-delimited string of the selected items and assigns
* that string to the edit box.
LPARAMETERS oDataObject, nEffect, nButton, nShift, ;
            nXCoord, nYCoord
LOCAL oSource, cResult, nCount

IF oDataObject.GetFormat("VFP Source Object")
   * We have a VFP data source, so get a reference
   oSource = oDataObject.GetData("VFP Source Object")
   * Is it a list? If so, loop through
   IF UPPER(oSource.BaseClass) = "LISTBOX"
      cResult = ""
      FOR nCount = 1 TO oSource.ListCount
         IF oSource.Selected[ nCount ]
            cResult = cResult + oSource.List[ nCount ] + ","
         ENDIF
      ENDFOR
      * Strip trailing comma
      cResult = SUBSTR(cResult, 1, LEN(cResult)-1)
      This.Value = cResult
      * Prevent normal dragdrop behavior
      NODEFAULT
   ENDIF
ENDIF

* See OLEDrag for an example of creating a custom format.

See Also

DataObject, GetData, OLE drag and drop, OLEDragDrop, OLESetData, OLEStartDrag, SetData