Drag and Drop between listboxes

I just extended the drag/drop capabilities of my app. I was expecting this to require some surgery on the code, but in the end it was relatively simple, just requiring adding two event handlers, one very short, and most of the code in the other was just for creating the drag-picture.

The two event handlers were:

DragRow - for the source listbox
DropObjectOnRow - for the taget listbox

This was for dragging a row from a listbox, dropping it on a row in an entrely different listbox (that is, the target listbox has a different purpose in the app than that of the listbox of the dragged row), and having the target listbox take some action.

This all worked OK, with this line of code:

drag.Text = "some string"

where “some string” had the required data to tell the target what to do. However, thinking about it caused me to then try dropping some random text from a TextArea onto the target listbox, which lo-and-behold accepted it and was not happy. To avoid this, I moved to using this:

drag.RawData("abcd") = "some string"

and looking for that raw-data on the receiving side. This technique I already use for all other drag/drop in my app, but the examples didn’t appear to imply that this could be done in this case - and, en plus, RawData and RawDataAvailable do not show up in the debugger as DragItem properties.

So far so good. But in addition to those properties not being visible in the debugger, it also seems that the drag text must be set - even if not used. It seems the drag-item must have its Text property set to something, even an empty string, otherwise the DropObjectOnRow does not fire on the target.

Anyone know why this might be? Seems unnecessary to me.

Did you set AcceptRawDataDrop?

https://documentation.xojo.com/api/user_interface/desktop/desktoplistbox.html#desktoplistbox-acceptrawdatadrop

No, for the target listbox I call AcceptTextDrop. But it seems to accept the raw-data drop anyway. I’ll try changing that. Can I call it mopre than once, such as:

me.AcceptRawDataDrop ("abcd")
me.AcceptRawDataDrop ("pqrs")

? There will be more than one type of drop occurring.

have-you checked “Drag and drop multiple rows” in the documentation ? That explain how to do it from a ListBox to another one…

Yes - it explains how to drop Text, which is no use at all, as it accepts text from anywhere.

in one of my app i store an object at drag in a module property and at drop i used it.

Switching to using AcceptRawDataDrop (times 2, in fact) and doing some extensive testing of both types of drag/drop I have implemented, all seems to function as it should. In fact, I found a subtle bug in the first type of drag/dro so that’s good. Easily fixed, fortunately, as this was drag re-ordering rows in a hierarchical listbox which represents a file system, so such reordering involves moving actual files and folders around, too. To this day I am unclear how I managed to make that work. Anyway thanks @StefanA for the push in the right direction.

1 Like