Click here to Skip to main content
14,980,370 members
Articles / Programming Languages / C#
Posted 25 Mar 2006


34 bookmarked

Draggable Form: Drag a Borderless Form by Clicking Anywhere on the Form

Rate me:
Please Sign up or sign in to vote.
4.88/5 (15 votes)
6 Apr 2006CPOL1 min read
Drag a borderless form by clicking anywhere on the form
Sample image


This is useful when you have a form without any border and you want to allow the user to drag it. This is a simple code and doesn't require any explanation at all. But I feel this will be helpful for at least some of you for your development. If you set the BorderStyle of a form to None, no title bar will be available for you to move the form. Here you can make use of this simple code to setup a base form and inherit all your form from this base form. After inheriting from this base form, you will be provided with two more properties in the Property Editor, Draggable (boolean) and ExcludeList (String). By setting the Draggable property to true, you are enabling the draggable feature of the form. You can pass a comma separated list of controls for which you don't want the draggable feature to be enabled. That is by default if Draggable=true, the form will be draggable by clicking anywhere on the form and any controls on the form. And naturally, you don't want the form draggable while a user clicks on a Button on it. So you can pass the name of the button to this list. E.g.: ExcludeList = "button1, button2, button3";.


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DraggableForm
    public class FormBase :Form
        #region Declarations
        private bool drag = false;
        private Point start_point = new Point(0, 0);
        private bool draggable = true;
        private string exclude_list = "";

        /// <span class="code-SummaryComment"><SUMMARY></span>
        /// Required designer variable.
        /// <span class="code-SummaryComment"></SUMMARY></span>
        private System.ComponentModel.IContainer components = null;

        #region Constructor , Dispose

        public FormBase()

        //Adding Mouse Event Handlers for the Form
        this.MouseDown += new MouseEventHandler(Form_MouseDown);
            this.MouseUp += new MouseEventHandler(Form_MouseUp);
            this.MouseMove += new MouseEventHandler(Form_MouseMove);

        /// <span class="code-SummaryComment"><SUMMARY></span>
        /// Clean up any resources being used.
        /// <span class="code-SummaryComment"></SUMMARY></span>
        /// true if managed resources should be disposed; otherwise, false.
        protected override void Dispose(bool disposing)
            if (disposing && (components != null))


        #region Windows Form Designer generated code

        /// <span class="code-SummaryComment"><SUMMARY></span>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// <span class="code-SummaryComment"></SUMMARY></span>
        private void InitializeComponent()
            // FormBase
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(369, 182);
            this.Name = "FormBase";
            this.Text = "AlerterForm";


        #region Overriden Functions

        protected override void OnControlAdded(ControlEventArgs e)
            //Add Mouse Event Handlers for each control added into the form,
            //if Draggable property of the form is set to true and the control
            //name is not in the ExcludeList.Exclude list is the comma separated
            //list of the Controls for which you do not require the mouse handler 
            //to be added. For Example a button.  
            if (this.Draggable && (this.ExcludeList.IndexOf(e.Control.Name) == -1))
                e.Control.MouseDown += new MouseEventHandler(Form_MouseDown);
                e.Control.MouseUp += new MouseEventHandler(Form_MouseUp);
                e.Control.MouseMove += new MouseEventHandler(Form_MouseMove);


        #region Event Handlers

        void Form_MouseDown(object sender, MouseEventArgs e)
            //On Mouse Down set the flag drag=true and 
            //Store the clicked point to the start_point variable
            this.drag = true;            
            this.start_point = new Point(e.X, e.Y);

        void Form_MouseUp(object sender, MouseEventArgs e)
            //Set the drag flag = false;
            this.drag = false;

        void Form_MouseMove(object sender, MouseEventArgs e)
            //If drag = true, drag the form
            if (this.drag)
                Point p1 = new Point(e.X, e.Y);
                Point p2 = this.PointToScreen(p1);
                Point p3 = new Point(p2.X - this.start_point.X, 
                                     p2.Y - this.start_point.Y);
                this.Location = p3;


        #region Properties

        public string ExcludeList
                this.exclude_list = value;
                return this.exclude_list.Trim();

        public bool Draggable
                this.draggable = value;
                return this.draggable;


Now, you can simply inherit your form from the FormBase and voila, it'll be draggable. If you have any suggestions, please mail me. I love to have friends and that's the reason I am here. Happy coding!


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


About the Author

Russell Aboobacker
Web Developer
India India
Russell Aboobacker is a Software Engineer from India, Currently working in Cognizant, Bangalore as Software Architect. He Enjoys Coding and Sharing his Experiences with the Colleagues and Friends.When he is not coding he enjoys spending time with his Family.

If you have any suggestions / Ideas , Share it With me.

Comments and Discussions

Questiongreat job Pin
Mustafa Magdy15-Jul-11 12:49
MemberMustafa Magdy15-Jul-11 12:49 
GeneralGreat!!! Pin
DavidDiego29-Dec-10 21:25
MemberDavidDiego29-Dec-10 21:25 
GeneralRemove handles to move Pin
CQüeb2-Apr-09 8:30
MemberCQüeb2-Apr-09 8:30 
GeneralRemoving the handles to move the form Pin
CQüeb2-Apr-09 8:27
MemberCQüeb2-Apr-09 8:27 
QuestionHow to move or drag a borderless form in Pin
The Ricmann14-Aug-08 1:35
MemberThe Ricmann14-Aug-08 1:35 
GeneralVery good for widgets and resx question Pin
milkplus7-Sep-07 5:39
Membermilkplus7-Sep-07 5:39 
GeneralTearing/Messy Painting Pin
xr280xr27-Aug-07 7:58
Memberxr280xr27-Aug-07 7:58 
GeneralThe Shortest Way Pin
RobertChan20006-Jul-06 8:43
MemberRobertChan20006-Jul-06 8:43 
GeneralThe Shortest Way (in VB.NET) Pin
RobertChan20006-Jul-06 8:52
MemberRobertChan20006-Jul-06 8:52 
GeneralRe: The Shortest Way (in VB.NET) Pin
kaushik25910628-Mar-07 20:33
Memberkaushik25910628-Mar-07 20:33 
GeneralRe: The Shortest Way [modified] Pin
unknowndentified1011111-Jul-11 18:45
Memberunknowndentified1011111-Jul-11 18:45 
GeneralA Shorter and Better Way Pin
georani26-Mar-06 3:27
Membergeorani26-Mar-06 3:27 
GeneralConverted to VB 2005 ( More Shorter Yet) Pin
georani26-Mar-06 3:42
Membergeorani26-Mar-06 3:42 
GeneralRe: Converted to VB 2005 ( More Shorter Yet) Pin
Väinölä Harri9-Apr-06 19:52
MemberVäinölä Harri9-Apr-06 19:52 
GeneralRe: Converted to VB 2005 ( More Shorter Yet) Pin
georani10-Apr-06 0:17
Membergeorani10-Apr-06 0:17 
GeneralWhen the FormBorderStyle is not equals to FormBorderStyle.None Pin
georani26-Mar-06 1:22
Membergeorani26-Mar-06 1:22 
GeneralGood job. Pin
Stanciu Vlad25-Mar-06 19:57
MemberStanciu Vlad25-Mar-06 19:57 
GeneralAlternate method Pin
Ravi Bhavnani25-Mar-06 7:04
professionalRavi Bhavnani25-Mar-06 7:04 
GeneralRe: Alternate method Pin
The_Mega_ZZTer25-Mar-06 7:12
MemberThe_Mega_ZZTer25-Mar-06 7:12 
GeneralRe: Alternate method Pin
Russell Aboobacker26-Mar-06 5:32
MemberRussell Aboobacker26-Mar-06 5:32 
AnswerRe: Alternate method Pin
Ravi Bhavnani26-Mar-06 5:45
professionalRavi Bhavnani26-Mar-06 5:45 
GeneralThis code is wrong, see the correct Pin
georani27-Mar-06 0:22
Membergeorani27-Mar-06 0:22 
GeneralRe: This code is wrong, see the correct Pin
Ravi Bhavnani27-Mar-06 1:00
professionalRavi Bhavnani27-Mar-06 1:00 
GeneralRe: This code is wrong, see the correct Pin
Russell Aboobacker28-Mar-06 1:57
MemberRussell Aboobacker28-Mar-06 1:57 
GeneralRe: This code is wrong, see the correct Pin
CQüeb2-Apr-09 7:25
MemberCQüeb2-Apr-09 7:25 

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

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