Move Controls With The Mouse On A Form At Runtime

In this article, we will learn how to move controls with the mouse on form at runtime in C#

Introduction

 
In this article, we will learn how to move controls with the mouse on form at runtime in C# windows applications.
 
In some cases, it is handy to move controls on a form around by using your mouse.
 
There is a helper class which does all the stuff needed to do this. Using this class, we can make movable controls with only one line of code.
  1. MoveControls.helperClassforCotrolMover.Init(this.groupBox1);   

What is a Helper Class?

 
In object-oriented programming, a helper class is used to assist in providing some functionality, which is not the main goal of the application or class in which it is used. An instance of a helper class is called a helper object. A helper class is just a term to refer to a class that provides methods that "help" do something. 
 
Definition
 
A helper class is a class filled with static methods. It is usually used to isolate a "useful" algorithm.
 
For example,
 
If you had to find the square root of a number multiple times within a method, you wouldn't write out the code to find the root each time you needed it, you would separate it out into a method like Math.Sqrt. 
 
In the same way,  we use the helper class in this below example to move the control at runtime.
 
Step 1
 
First, create the new Windows form application project
 
 
Step 2
 
Design the form. In this example I use the groupBox, label, and button control. You can take any controls which you want to move.
 
 
Step 3
 
Then add a new class. In my case, I have created the helperClassforControlMover.cs class to move the controls and write down the below code.
 
This code uses anonymous delegates to do the hard work. One advantage of this is that helper class helperClassforControlMover has only static methods.
 
 
Use this code in a helper class, 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Windows.Forms;  
  6. using System.Drawing;  
  7. namespace MoveContols {  
  8.     class helperClassforControlMover {  
  9.         public enum direction {  
  10.             Any,  
  11.             Horizontal,  
  12.             Vertical  
  13.         }  
  14.         public static void Init(Control control) {  
  15.             Init(control, direction.Any);  
  16.         }  
  17.         public static void Init(Control control, direction direction) {  
  18.             Init(control, control, direction);  
  19.         }  
  20.         public static void Init(Control control, Control container, direction direction) {  
  21.             bool Dragging = false;  
  22.             Point DragStart = Point.Empty;  
  23.             control.MouseDown += delegate(object sender, MouseEventArgs e) {  
  24.                 Dragging = true;  
  25.                 DragStart = new Point(e.X, e.Y);  
  26.                 control.Capture = true;  
  27.             };  
  28.             control.MouseUp += delegate(object sender, MouseEventArgs e) {  
  29.                 Dragging = false;  
  30.                 control.Capture = false;  
  31.             };  
  32.             control.MouseMove += delegate(object sender, MouseEventArgs e) {  
  33.                 if (Dragging) {  
  34.                     if (direction != direction.Vertical) container.Left = Math.Max(0, e.X + container.Left - DragStart.X);  
  35.                     if (direction != direction.Horizontal) container.Top = Math.Max(0, e.Y + container.Top - DragStart.Y);  
  36.                 }  
  37.             };  
  38.         }  
  39.     }  
  40. }  
 
Step 4
 
An example of how to use this code is presented in the project source file. All controls can be moved by a mouse.
 
To enable moving mode for control, we just call the Init method in helperClassforMoveControl class and send control to it. 
 
 
Step 5
 
Then run the program and see the output. Now control can be moved at runtime. I have shown two output images, Output 1 is first, and after moving control the output in the second image is output 2. 
 
 
 
Note 
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.
See the below code.
  1. MoveControls.helperClassforCotrolMover.Init(this.groupBox1);  
  2. MoveControls.helperClassforCotrolMover.Init(this.groupBox2, MoveControls.helperClassforCotrolMover.Direction.Any);  
  3. MoveControls.helperClassforCotrolMover.Init(this.groupBox2, MoveControls.helperClassforCotrolMover.Direction.Horizontal);  
  4. MoveControls.helperClassforCotrolMover.Init(this.groupBox2, MoveControls.helperClassforCotrolMover.Direction.Vertical);  

Summary

 
In this article, we discussed how to move controls with the mouse at runtime in C# windows form applications.
 
We learned about helper class, and how we use a helper class to do this operation.
 
To  enable moving mode for control, we call the Init method in helperClassforMoveControl class and send control to it.  
 
I hope this article will be useful for you!
 
Source code is attached to this article.