Click here to Skip to main content
Click here to Skip to main content
Go to top

Move controls on a form at runtime

, 17 Dec 2008
Rate this:
Please Sign up or sign in to vote.
Move controls on a form at runtime by just using a helper class and one line of code.


In some cases, it is handy to move controls on a form around by using your mouse. In this project, there is a helper class which does all the stuff needed to do this. Not only can a control be moved, but also its container.

Only one line of code is used to make a control movable (not ugly drag and drop):


Really? Yes!!


This code uses anonymous delegates to do the hard work. One advantage of this is that the helper class ControlMover has only static methods.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Helper
  class ControlMover
    public enum Direction

    public static void Init(Control control)
      Init(control, Direction.Any);

    public static void Init(Control control, Direction direction)
      Init(control, control, direction);

    public static void Init(Control control, Control container, Direction direction)
      bool Dragging = false;
      Point DragStart = Point.Empty;
      control.MouseDown += delegate(object sender, MouseEventArgs e)
        Dragging = true;
        DragStart = new Point(e.X, e.Y);
        control.Capture = true;
      control.MouseUp += delegate(object sender, MouseEventArgs e)
        Dragging = false;
        control.Capture = false;
      control.MouseMove += delegate(object sender, MouseEventArgs e)
        if (Dragging)
          if (direction != Direction.Vertical)
            container.Left = Math.Max(0, e.X + container.Left - DragStart.X);
          if (direction != Direction.Horizontal)
            container.Top = Math.Max(0, e.Y + container.Top - DragStart.Y);

Using the Code

An example how to use this code is presented in the project source file. All controls can be moved by a mouse. Also, a simple splitter using a toolbar is presented. Another panel containing a toolbar can be moved by using its toolbar.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace MoveYourControls
  public partial class Form1 : Form
    public Form1()

      // Make your controls movable by a mouseclick

      // Move a panel by its toolstrip
      Helper.ControlMover.Init(this.toolStrip2, this.panel3, 

      // Make a splitter from toolstrip
      Helper.ControlMover.Init(this.toolStrip1, Helper.ControlMover.Direction.Vertical);
      this.toolStrip1.LocationChanged += delegate(object sender, EventArgs e)
        this.panel1.Height = this.toolStrip1.Top;

The Form after moving all the controls around.

Points of Interest

Sometimes, a control may only be moved in one direction. This is true for splitters and stuff. The helper class has a direction enumerator which makes things really easy:

Helper.ControlMover.Init(this.button2, Helper.ControlMover.Direction.Any);
Helper.ControlMover.Init(this.button3, Helper.ControlMover.Direction.Horizontal);
Helper.ControlMover.Init(this.button4, Helper.ControlMover.Direction.Vertical);

Have fun!!


As of publication, version is presented.


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


About the Author

Alphons van der Heijden
Retired Van der Heijden Holding BV
Netherlands Netherlands
I'm Alphons van der Heijden, living in Lelystad, Netherlands, Europa, Earth. And currently I'm retiring from hard working ( ;- ), owning my own company. Because I'm full of energy, and a little to young to relax ...., I don't sit down, but create and recreate software solutions, that I like. Reinventing the wheel is my second nature. My interest is in the area of Internet technologies, .NET etc. I was there in 1992 when Mosaic came out, and from that point, my life changed dramatically, and so did the world, in fact. (Y)

Comments and Discussions

QuestionGreat article PinmemberBinu198516-Sep-14 20:18 
Questionadding a button PinmemberMember 1088634415-Jun-14 18:45 
GeneralPerfect my friend!! What I needed! PinmemberAngelos_Alexopoulos13-Jun-14 2:23 
Suggestionnew article on base of your article [modified] Pinprofessionalzomorrod.company8-Feb-14 23:26 
GeneralThe Best Pinmemberspaceman8129-Jan-14 7:19 
GeneralRe: The Best PinprofessionalAlphons van der Heijden29-May-14 22:23 
QuestionAdd arrow keys Pinmembert_nedelchev8-Oct-13 21:48 
AnswerRe: Add arrow keys PinmemberAlphons van der Heijden9-Oct-13 6:31 
GeneralRe: Add arrow keys Pinmembert_nedelchev9-Oct-13 20:35 
GeneralRe: Add arrow keys Pinmemberspaceman8129-Jan-14 7:13 
GeneralRe: Add arrow keys PinprofessionalAlphons van der Heijden29-May-14 22:23 
QuestionMoving the control in Java PinmemberSharmaShruthi28-Aug-13 1:22 
AnswerRe: Moving the control in Java PinmemberAlphons van der Heijden30-Aug-13 2:25 
GeneralMy vote of 5 PinmemberMember 1021971719-Aug-13 20:51 
GeneralRe: My vote of 5 PinmemberAlphons van der Heijden30-Aug-13 2:25 
GeneralMy vote of 5 PinmemberJayanta Chatterjee19-Aug-13 4:57 
QuestionWhat about ListView? PinmemberSergeyAndreyev7-May-13 7:45 
GeneralMy vote of 5 Pinmemberlucianomalta1-May-13 11:03 
GeneralMy vote of 5 Pinmemberkevininstructor27-Mar-13 15:53 
GeneralMy vote of 4 Pinmemberymmt25-Mar-13 20:46 
Questionmoving showing line like in Forms designer Pinmemberrmircea7-Feb-13 23:38 
AnswerRe: moving showing line like in Forms designer PinmemberAlphons van der Heijden8-Feb-13 1:11 
Questionit's cool .i like.thanks a lot Pinmemberkankankan22222-Dec-12 1:56 
QuestionGood one PinmemberDorababu74312-Dec-12 23:51 
AnswerRe: Good one PinmemberAlphons van der Heijden14-Dec-12 0:01 
GeneralRe: Good one PinmemberMember 1036187016-Dec-13 4:47 
QuestionGreat! A Question! PinmemberOnur Guzel23-Oct-12 22:44 
AnswerRe: Great! A Question! PinmemberAlphons van der Heijden23-Oct-12 23:18 
QuestionIs the best ! PinmemberMember 399319811-Oct-12 9:06 
QuestionNeed same example in VB.Net PinmemberMalikJalil27-Jun-12 2:53 
AnswerRe: Need same example in VB.Net PinmemberAlphons van der Heijden27-Jun-12 23:43 
AnswerRe: Need same example in VB.Net Pinmemberfreakyit30-Jun-12 8:53 
AnswerRe: Need same example in VB.Net Pinmemberkevininstructor27-Mar-13 15:52 
QuestionGreat! PinmemberMember 84305986-Mar-12 21:26 
QuestionGood! PinmemberAlex Park7-Feb-12 11:01 
GeneralMy vote of 5 PinmemberArmando de la Torre17-Oct-11 13:26 
GeneralMy vote of 5 Pinmemberaswind010116-Jul-11 17:15 
Questionplz help to stop a movable control..... Pinmemberkaushik24025-Jul-11 20:10 
AnswerRe: plz help to stop a movable control..... PinmemberAlphons van der Heijden19-Jul-11 3:50 
GeneralThanx PinmemberAlesmol15-Apr-11 21:02 
Generalthnk Pinmembersandeepparekh9-Mar-11 20:55 
GeneralSo thnx very nice work PinmemberIran1-Sep-10 10:25 
GeneralRe: So thnx very nice work PinmemberAlphons van der Heijden1-Sep-10 12:35 
Generalmoving the runtime controls Pinmembersivakumarmr1021-Aug-10 3:45 
GeneralRe: moving the runtime controls PinmemberAlphons van der Heijden1-Sep-10 12:33 
GeneralMy vote of 5 PinmemberToli Cuturicu19-Aug-10 5:01 
GeneralRe: My vote of 5 PinmemberAlphons van der Heijden1-Sep-10 12:35 
GeneralMy vote of 5 Pinmemberfishland_ls20-Jul-10 6:24 
Generalneed some help.... PinmemberViper-Eyes16-Feb-10 4:34 
GeneralRe: need some help.... PinmemberAlphons van der Heijden4-Jun-10 12:02 

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 | Mobile
Web04 | 2.8.140916.1 | Last Updated 17 Dec 2008
Article Copyright 2008 by Alphons van der Heijden
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid