Drag and Drop in Windows Forms - A Primer

The ListView ctnd.

The first difference here is that instead of the mousedown event we use the ItemDrag event which is what we use for both the ListView and the TreeView. And as before we initiate the D&D by using the DoDragDrop method and in this case add the item, e.Item. The ItemDragEventArgs allows us to do this and is indeed very useful. The DragEnter event is the same as before so we can go directly to the meat and veg. of the whole process i.e. the DragDrop event. I could just add items to the relevant ListView control but it's perhaps more realistic to implement the ability to add the item where we were hovering. This means we need a reference to the item we are dragging and the item we are hovering over (or nearest to). The item we are dragging is in the clipboard and can be retrieved using the GetData method of the Data object. The useful thing about the GetData method is that once we know what kind of object we want we just pass it the name of the object in a string as above, e.Data.GetData("System.Windows.Forms.ListViewItem").

We get the item we are hovering over by using the GetItemAt method of the ListView. This method requires client co-ordinates and we get this by a round about way. First we get the point itself by using the PointToClient of the Listview. This takes a point as it's argument so we can hand it a point generated by the X and Y of the DragEventArgs and from there the X and Y of this new client point are the values we need for the GetItemAt method. We can then use the index of this item, destItem, in the Insert method of the Items collection of the ListView (still with me?). The Insert method is a nice method that inserts your new item into the index supplied and bumps the other items down. Finally we just remove the item from it's parent ListView. If you have problems understanding how all this is working feel free to contact me and also have a look at the example project with this article which might help to shed more light on the matter. (N.B. I am assuming that wherever I post this article I can include my example project. If this is not the case then email me and I will be happy to send it to you.)

So that's the ListView. You can apply this technique to allow dragging items to other controls like textboxes. All you need then is the text value of the dragged ListViewItem, lvItem.Text in the above example. So you can take this example and extend upon it for your own ends. My final example is the one that caused me the most problems so with out further ado...

You might also like...

Comments

About the author

Brian O'Connell Ireland

Microsoft Certified Applications Developer with 10 years experience developing web based applications using asp, asp.net for a Local Authority in Dublin. Clings to a firm belief that a web appli...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves” - Alan Kay