C# Programming > Windows Forms

C# Mouse-Drag Windows Form


Drag Windows Form

There are numerous ways to create custom shaped Windows Forms in C#, and coupled with flashy images one can create truly professional and innovative user interfaces.

However a key element in any custom Form behavior is the ability to click and mouse drag the C# Windows Form. This task be easily accomplished using a couple of C# events.

The Form C# Events

The general concept of the C# code is to capture the cursor’s position when it clicks the Windows Form (or a control inside the Form). Then as the cursor moves (as long as the mouse is still clicked) compare the new position with the captured one...

The difference in the positions is then applied to the Form’s Position.

Use the following C# events:

MouseDown – In this event capture the mouse position (with the object e) and assign it to a Point field.

MouseMove – First make sure to check that the mouse bottom pressed is the Left one (or the Right one, as long as one is pressed). Then use this e’s position and subtract it from the previous Point field. Apply the X and Y differences to the Form’s Left and Top properties.


Note that if you wish for this behavior to apply to everything on the Form you will need to use every control’s MouseDown and MouseMove C# event.

Moreover instead of applying the behavior to the entire window the concept will apply with just a control. In other words to only the area in which a mouse clicks.

These events are also part of the other .NET languages and will create the same effect as a C# Windows Form.

Back to C# Article List