Hi all,
I am currently writing an application in VB.NET that has a lot of controls (treeviews/listviews/labels and so forth) that represent "things" that need to be draggable from place to place, including between controls and windows.
In the past when I've implemented Drag and Drop in other projects, I've always passed the item being dragged (for example the listviewitem or the treenode) to the DoDragDrop and then retrieved it in the DragDrop event handler to find out what's been dropped.
This of course means making a special case for each type of droppable object (the listviewitem, tree node or whatever) and then working out from stuff I've put in it's .tag what the "thing" is that the dropped item refers to.
In this project I could be dropping all sorts of controls, labels, textboxes, pictureboxes, treenodes, listviewitems and so on, and I don't really care what the forms object being dropped is, all I care about is what data that object represents in my application model.
The easy way seems to be not to pass the forms object being dragged to the DoDragDrop function, but to pass some user-data instead. That way the receipient of the drop can tell what the data being dragged is, and doesn't really care what the physical forms object is. I'm never going to 'move' the forms object being dragged, I'm going to change the data model, and then redraw things.
It seems to work ok if I pass a System datatype like a string or a long, I can retrieve the data in the DragDrop event of the target control using GetData, however I could really do with passing a user-defined structure rather than an intrinsic data-type.
I can't get that to work. I can pass the structure to DoDragDrop ok - that works, but I can't find a way to get it back at the other end, from the e.data returned in the DragDrop event.
I can't seem to find a conversion that will work on e.data that will convert the data to something of type "myproject.mystructure". Fine if I pass a system data type, conversions to "System.String" work for example, so I've resorted to converting my structure to a string, passing that to DoDragDrop, and turning it back again in the events. It's not very efficient, especially as I want to know what is being passed in other events like DragEnter as well, so I'm doing a lot of converting, and if what I'm dragging is a multiple-selection then the conversion to a string is a real pain.
Is there a way to pass a user-defined structure to DoDragDrop and get it back again in DragDrop at the target end? Then I could always pass an array of mystructure (which might only contain one element) by reference to every Drag & Drop operation in the whole application, and it would be much neater.
Regards,
Steve