By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,212 Members | 1,107 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,212 IT Pros & Developers. It's quick & easy.

Passing data for Drag & Drop

P: 3
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 returned in the DragDrop event.

I can't seem to find a conversion that will work on 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.

Feb 10 '07 #1
Share this Question
Share on Google+
1 Reply

P: 3
In reply to my own thread, I suddenly realised that all this passing of data in order to know what object(s) are being dragged is really unnecessary.

There can only ever be one Drag and Drop operation in progress at any given time, so I might as well build my list of objects in global scope before I call DoDragDrop, and then I can access them from anywhere I like.

This makes the whole thing a lot simpler and faster than trying to pass data all over the place.

It's maybe not as neat as 'sending' the data along with DoDragDrop, but it's a lot faster and a lot less messy, and the structure I build to represent the dragged data can be anything I like.
Feb 11 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.