Click here to Skip to main content
Click here to Skip to main content

Tagged as

Draggable WinForms Controls

, 8 Apr 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Extension method to make any WinForms control to be draggable at runtime

Sometimes, you need to make the control draggable during runtime. It can be a form with FormBorderStyle property set to None, or any other situation.

I want to propose a simple extension method that allows you to make this possible.

using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace DraggableControls
    public static class ControlExtension
        // TKey is control to drag, TValue is a flag used while dragging
        private static Dictionary<Control, bool> draggables = 
                   new Dictionary<Control, bool>();
        private static System.Drawing.Size mouseOffset;
        /// <summary>
        /// Enabling/disabling dragging for control
        /// </summary>
        public static void Draggable(this Control control, bool Enable)
            if (Enable)
                // enable drag feature
                if (draggables.ContainsKey(control))
                {   // return if control is already draggable
                // 'false' - initial state is 'not dragging'
                draggables.Add(control, false);
                // assign required event handlersnnn
                control.MouseDown += new MouseEventHandler(control_MouseDown);
                control.MouseUp += new MouseEventHandler(control_MouseUp);
                control.MouseMove += new MouseEventHandler(control_MouseMove);
                // disable drag feature
                if (!draggables.ContainsKey(control))
                {  // return if control is not draggable
                // remove event handlers
                control.MouseDown -= control_MouseDown;
                control.MouseUp -= control_MouseUp;
                control.MouseMove -= control_MouseMove;
        static void control_MouseDown(object sender, MouseEventArgs e)
            mouseOffset = new System.Drawing.Size(e.Location);
            // turning on dragging
            draggables[(Control)sender] = true;
        static void control_MouseUp(object sender, MouseEventArgs e)
            // turning off dragging
            draggables[(Control)sender] = false;
        static void control_MouseMove(object sender, MouseEventArgs e)
            // only if dragging is turned on
            if (draggables[(Control)sender] == true)
                // calculations of control's new position
                System.Drawing.Point newLocationOffset = e.Location - mouseOffset;
                ((Control)sender).Left += newLocationOffset.X;
                ((Control)sender).Top += newLocationOffset.Y;


It's pretty simple to use this code. To make control draggable, you just need to write one line of code:


and to disable this feature:


For example, if your Form contains two checkboxes named checkBox_DragForm and checkBox_DragButton with CheckedChanged event handlers assigned, and button with name btnTest, you may use the following code to enable/disable drag feature on Form (represented by this) or/and button:


private void checkBox_DragForm_CheckedChanged(object sender, EventArgs e)
private void checkBox_DragButton_CheckedChanged(object sender, EventArgs e)


The code sample is here (VS2010 solution).

Hope this would be helpful.

Update (2015-02-26): Extention is also available on Github and as a Nuget package


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer DevelopEx
Ukraine Ukraine
No Biography provided

Comments and Discussions

QuestionClick Event PinmemberManvendra Singh23-Feb-15 20:38 
AnswerRe: Click Event [modified] Pinmemberintrueder26-Feb-15 0:10 
GeneralRe: Click Event [modified] PinmemberManvendra Singh26-Feb-15 0:57 
AnswerRe: Click Event [modified] Pinmemberintrueder26-Feb-15 3:42 
GeneralRe: Click Event PinmemberManvendra Singh26-Feb-15 19:07 
GeneralMy vote of 5 Pinmembernirav raval13-Feb-15 2:09 
AnswerRe: My vote of 5 Pinmemberintrueder26-Feb-15 8:32 
GeneralMy vote of 5 PinmemberErik Norman9-Feb-15 4:20 
GeneralMy vote of 5 PinmemberKeyi Zhang13-Jul-13 0:12 
GeneralMy vote of 5 PinmemberHorseman270323-Jan-13 23:18 
AnswerRe: My vote of 5 PinmemberInTRUEdeR24-Jan-13 0:55 
GeneralMy vote of 5 PinmemberLeszekCode27025-Oct-12 22:24 
GeneralMy vote of 5 Pinmembertruongvu_12325-Jun-12 0:20 
GeneralReason for my vote of 5 easy to use PinmemberCalvinWang19-Dec-11 0:17 
GeneralReason for my vote of 5 Very nice. Thanks Pinmemberzenwalker198519-Dec-11 0:15 
GeneralReason for my vote of 5 Excellent!! thank you very Pinmemberjawed.ace28-Jun-11 0:01 
GeneralReason for my vote of 5 Nice one..thanks!! Pinmemberjawed.ace25-Jun-11 22:23 
Reason for my vote of 5
Nice one..thanks!!
GeneralRe: thanks for your vote! PinmemberInTRUEdeR26-Jun-11 5:02 
Generalwow! I like it Pinmemberzhanghaocol1-May-11 23:44 
GeneralReason for my vote of 5 Nice and usefull snippet PinmemberDotNetMastermind12-Apr-11 1:15 
GeneralReason for my vote of 5 Simple, like it. PinmemberKim Togo11-Apr-11 23:16 
GeneralReason for my vote of 5 Cool! Wish I needed to use this some... PinmemberIGood11-Apr-11 14:54 
GeneralMy vote 5 This was really nice. PinmemberJAxelsson10-Apr-11 22:35 
GeneralReason for my vote of 5 Nice PinmemberHiMik200310-Apr-11 21:57 
GeneralReason for my vote of 5 Elegant and simple. Nice. PinmemberChris Trelawny-Ross8-Apr-11 8:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 8 Apr 2011
Article Copyright 2011 by intrueder
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid