Here’s a piece of code I wrote a while ago, I just thought it could be useful for WinForms developers…
In WPF, there is a very handy method to move a window with no borders : Window.DragMove
. It can be used like that :
private void Window_MouseDown(object sender, MouseButtonEventArgs e) { this.DragMove(); }
When you call this method, the window is moved with the mouse until the button is released. It could hardly be simpler 😉
Unfortunately, this method only exists in WPF, and a majority of developers are still working with Windows Forms. So I came up with a solution to use a similar technique in Windows Forms, with a few improvements :
- Usable on any control, not only a window
- No need to explicitly handle the
MouseDown
event - Form designer integration, using a
IExtenderProvider
My solution consists of the following items :
- a static
DragMoveExtensions
class which provides extension methods for theControl
class (easily convertible to regular static methods for use with C# 2) - a
DragMoveProvider
component, which implementsIExtenderProvider
to add a newEnableDragMove
property to controls
There are several ways to use this solution, pick the one that best suits your needs :
- The simplest, which requires no coding at all : in design mode, drop a
DragMoveProvider
on the Form, and set theEnableDragMove
property totrue
on the Form or control - The closest to WPF’s DragMove : in the handler of the
MouseDown
event, call theDragMove
extension method on the Form or control to move

private void label2_MouseDown(object sender, MouseEventArgs e) { label2.DragMove(); }
EnableDragMove
extension method on the Form or control to move (no event handling needed).private void checkBox1_CheckedChanged(object sender, EventArgs e) { this.EnableDragMove(checkBox1.Checked); }
The attached Visual Studio solution contains the WinFormsDragMove library, and a test project to demonstrate the various ways to use this library. A C#2-compatible version of these projects is also included.
Excellent Article on using Drag & Drop, with a disposable extension.
Thx!
I copied provider.cs and extensions.cs into my project, dragged a provider onto my GUI, enabled the property on the controls I wanted and had good movement!
My use for this is re-arranging the “dominos” in a user”s “hand” for a Trains game.
I have tried to add this to my program and i get nothing but errors. I have tried adding the project and I have tried just adding the files themselves and I have tried copy/paste. I am writing a simple game called knights tour. where the user moves the knight around from square to square.
Your code works perfectly when I just run your supplied project, but any which way I try to add it into my own project, nothing but errors.
I am new to c# and have learned a lot, but i seriously don”t know why i can”t add this bit to my code so my game will work better when the knight moves around.
InitializeComponent(); throws an error cause it says the function isn”t there when I add just the code as 2 more classes. As far as custom controls, I have two others in play already. This is the other reason this doesn”t make sense.
Can you please help?
I am a student that will be returning to college soon and just wanting to get a leg up on the stuff before i get there. ill happily email you my little project so you can help me figure this out. i can move the knight, but it is bad ugly and i believe this code is just what i need. I am just not sure how to implement it. 🙁
Thank you.
Thanks for this, saved me some time.
Eventually I needed to support a UserControl with children that I wanted to support drag-move behavior with and while what you provided works (and well, thanks again) when you click on the UserControl directly, clicking on child controls (say a flow layout panel) doesn’t work.
Good news is your extension is readily amended; I added a version of the DragMove extension function that takes a “List children” parameter so specific child controls can be enabled for the drag-move behavior.
If you email me somewhere to post/send a zip, I’ll provide the modifications back to you once I get them tidied up.
Can you email me a modification by you?
thank you
my email:chinasmu@163.com
Hi. I see this was several years ago. Will this now work in a web form or are we stuck in wpf? Thanks!
Hi Kevin,
I’m not sure I understand what you’re asking. This code works for WinForms and only for WinForms. A web implementation of this would be completely different (and would be in Javascript, not in C#, anyway…)
Hi,thanks you good work, but maybe i found a bug.
When i use your dll and set the Winform Startpostion as CenterScreen, it didnot work. The Winform still shows WindowsDefaultLocation.
Can this fixed?
Thank you
Strange… I don’t see how this code could affect the form’s StartPosition. If it *is* caused by my code, then I guess it can be fixed, but honestly, this is very old code, and I haven’t used WinForms in years, so I’m not really inclined to investigate the problem…