65.9K
CodeProject is changing. Read more.
Home

Move Controls on a Form at Runtime

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.92/5 (92 votes)

Dec 17, 2008

CPOL

1 min read

viewsIcon

816576

downloadIcon

17436

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

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.

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.

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:

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.