Click here to Skip to main content
15,885,366 members
Articles / Desktop Programming / Windows Forms

Move Controls on a Form at Runtime

Rate me:
Please Sign up or sign in to vote.
4.92/5 (92 votes)
17 Dec 2008CPOL1 min read 805.8K   17.3K   124   83
Move controls on a form at runtime by just using a helper class and one line of code

Introduction

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):

C#
Helper.ControlMover.Init(this.button1);

Really? Yes!!

Background

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

C#
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Helper
{
  class ControlMover
  {
    public enum Direction
    {
      Any,
      Horizontal,
      Vertical
    }

    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 of 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.

C#
using System;
using System.Drawing;
using System.Windows.Forms;

namespace MoveYourControls
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();

      // Make your controls movable by a mouseclick
      Helper.ControlMover.Init(this.button1);
      Helper.ControlMover.Init(this.checkBox1);
      Helper.ControlMover.Init(this.groupBox1);
      Helper.ControlMover.Init(this.textBox1);
      Helper.ControlMover.Init(this.label1);

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

      // 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:

C#
Helper.ControlMover.Init(this.button1);
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!!

History

  • As of publication, version 1.0.0.0 is presented.

License

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


Written By
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

 
AnswerRe: plz help to stop a movable control..... Pin
Alphons van der Heijden19-Jul-11 3:50
professionalAlphons van der Heijden19-Jul-11 3:50 
GeneralThanx Pin
Alesmol15-Apr-11 21:02
Alesmol15-Apr-11 21:02 
Generalthnk Pin
sandeepparekh9-Mar-11 20:55
sandeepparekh9-Mar-11 20:55 
GeneralSo thnx very nice work Pin
Iran1-Sep-10 10:25
Iran1-Sep-10 10:25 
GeneralRe: So thnx very nice work Pin
Alphons van der Heijden1-Sep-10 12:35
professionalAlphons van der Heijden1-Sep-10 12:35 
Generalmoving the runtime controls Pin
sivakumarmr1021-Aug-10 3:45
sivakumarmr1021-Aug-10 3:45 
GeneralRe: moving the runtime controls Pin
Alphons van der Heijden1-Sep-10 12:33
professionalAlphons van der Heijden1-Sep-10 12:33 
GeneralMy vote of 5 Pin
Toli Cuturicu19-Aug-10 5:01
Toli Cuturicu19-Aug-10 5:01 
ControlMover class may be marked static (improves performance)
GeneralRe: My vote of 5 Pin
Alphons van der Heijden1-Sep-10 12:35
professionalAlphons van der Heijden1-Sep-10 12:35 
GeneralMy vote of 5 Pin
fishland_ls20-Jul-10 6:24
fishland_ls20-Jul-10 6:24 
Generalneed some help.... Pin
Viper-Eyes16-Feb-10 4:34
Viper-Eyes16-Feb-10 4:34 
GeneralRe: need some help.... Pin
Alphons van der Heijden4-Jun-10 12:02
professionalAlphons van der Heijden4-Jun-10 12:02 
QuestionUn-initialize the control? Pin
kopflos5-Jul-09 4:59
kopflos5-Jul-09 4:59 
AnswerRe: Un-initialize the control? Pin
Alphons van der Heijden6-Jul-09 23:33
professionalAlphons van der Heijden6-Jul-09 23:33 
GeneralRe: Un-initialize the control? Pin
kopflos8-Jul-09 9:11
kopflos8-Jul-09 9:11 
GeneralGreat article....... Pin
soumyasurya19-Mar-09 1:13
soumyasurya19-Mar-09 1:13 
QuestionVery very good, but how can i move a control into a contrainer ? Pin
fady_sayegh9-Mar-09 4:30
fady_sayegh9-Mar-09 4:30 
AnswerRe: Very very good, but how can i move a control into a contrainer ? Pin
Alphons van der Heijden14-Mar-09 2:24
professionalAlphons van der Heijden14-Mar-09 2:24 
QuestionVery impressive, I like it, but what if I want to also resize controls? Pin
dubem44-Jan-09 8:09
dubem44-Jan-09 8:09 
AnswerRe: Very impressive, I like it, but what if I want to also resize controls? [modified] Pin
Alphons van der Heijden5-Jan-09 21:51
professionalAlphons van der Heijden5-Jan-09 21:51 
GeneralRe: Very impressive, I like it, but what if I want to also resize controls? Pin
Pamodh3-Sep-10 6:50
Pamodh3-Sep-10 6:50 
GeneralDirection as bit flags Pin
TobiasP25-Dec-08 4:26
TobiasP25-Dec-08 4:26 
GeneralRe: Direction as bit flags Pin
Alphons van der Heijden28-Feb-09 1:25
professionalAlphons van der Heijden28-Feb-09 1:25 
GeneralAmazing! Pin
ctrlV23-Dec-08 10:46
professionalctrlV23-Dec-08 10:46 
GeneralRe: Amazing! Pin
Alphons van der Heijden23-Dec-08 11:28
professionalAlphons van der Heijden23-Dec-08 11:28 

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.