Recently I have been working on a project that required a list box that would allow data to dragged and dropped between the two. My first thought was to simply add the required events to a ListBox control and generate the code for the drag and drop operation. The problem with this concept is that the project required several of these controls and duplicating the code for each event or distinguishing between which control generated the event would work but didn't seem real clean to me.
To me the best solution was to create a class that was derived from ListBox and then simply add the code for the drag and drop operation to it. Once I had the class setup I had to go about learning how .NET handled drag and drop operations. I scoured the Internet and found a great deal of articles that covered this but many of them were a little ambiguous or flat out didn't work well. Fortunately I came across the following article by Nishant Sivakumar
titled Drag and Drop between list boxes - Beginner's Tutorial
. I found this to be a very well written article and I would like to thank the author for publishing it so that we can all learn from it.
The control for this project is in a dll format which you can download here.
It should be noted that this code was written with Visual Studio 2008, was created as a debug build, and include the debug database in the zip for those who need to do some debugging.
Using the Control
To use the control you must first add it to the Visual Studio Tool box. To do this simply right click somewhere in the tool box and select "Choose Items" This will open up the following dialog window:
From here you simply make sure the .NET Framework Components tab is selected, and hit the browse button to navigate to the folder where you unzipped the control. You should select the file DDLisBox.dll. That is it, the control should have shown up in the Toolbox in the general section:
You will notice that I created an icon for the toolbox and you can also tell that I am not the greatest icon desinger in the world but it serves its purpose and is probably a little better than the default icon.
A couple of things on using the control
1. I have added a property to the properties window called Copy. If you set this value to true data in the DragDropListBox will be copied to the drag target. This means it will still remain in the drag source. If you want to move the data from DragDropListBox to DragDropListBox simply set this value to false.
2. There may be times when you want the control to be a drag source only. This means that you don't want the control to have the ability to have data dropped into it. In this case set the AllowDrop property to false.
That is about it. I haven't included the source code but anyone who want it is free to have it. Simply drop me an email and I will arrange to get it to you. Also, if you find there are problems with the code or want to suggest some features simply comment here and I will see what I can do.