Drag and Drop in Windows Forms - A Primer


Ah drag and drop. Now wouldn't it be impressive if my user could drag items all over the place and drop them anywhere they wished. Well ok steady on. Maybe just where it would be useful. Right then to the web where I can find out how easy it is to do this in Windows Forms. And so the problems begin. Drag and drop is a very familiar visual aid in Windows operating systems and it is fairly straight-forward to apply the technique in .net but try and find a good example and you'll probably get what I got - how to drag some text from one textbox to another or something along those lines. Come on we want to know how to drag treeview items from one to another, how to move files into a folder in a listview or even better how to be able to move a control around a form. Well sit back and I'll show you how I did it. When you're experimenting with these things you might not necessarily come up with the best way of doing things but you certainly find a good starting point from which to go on. So I hope you can do exactly that.

Why Drag and Drop?

Err because I want to Drag and Drop. Well ok I know that but allowing a user to click on something and move it to something else can be done using the mouse events and a few Boolean flags. Consider the sequence of events.

  1. Trap the MouseDown event - did the user click the left mouse button? If so set our private dragging flag/boolean to true.
  2. Trap the MouseMove event - are we dragging? (flag=true) then implement some way of visually showing the item to be moving with the mouse or do nothing and wait for.......
  3. Trap the MouseUp event - are we dragging? (flag=true) well then code the item to appear where the mouse stopped and hey presto.

So why use Drag and Drop events? Well it brings us back to the visual side of things. Drag and Drop events allow us to visually show that a Drag and Drop operation is in effect with little mouse indicators that say "Keep going buddy. you can't park that here" or "Welcome to Chez Drop" etc. And we are good Windows-fearing people who want to do it just like everybody else. Lets face it though it's the sensible way to do it. These events also allow us to copy the object we are moving to the clipboard and implement the D&D effect so we can use less code to achieve the same effect. So lets see the Drag and Drop sequence of events in, the horror, code using the crappy text example I mentioned earlier.

You might also like...


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.


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.

“Never trust a programmer in a suit.” - Anonymous