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.
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 the 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 group box, 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 helperClassforControlMoverhas only static methods.
Use this code in a helper class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace MoveContols
{
class helperClassforControlMover
{
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);
}
};
}
}
}
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 the 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.
Figure 1: Output 1
Figure 2: 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.
MoveControls.helperClassforCotrolMover.Init(this.groupBox1);
MoveControls.helperClassforCotrolMover.Init(this.groupBox2, MoveControls.helperClassforCotrolMover.Direction.Any);
MoveControls.helperClassforCotrolMover.Init(this.groupBox2, MoveControls.helperClassforCotrolMover.Direction.Horizontal);
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 the 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 the helperClassforMoveControl class and send control to it.
I hope this article will be useful for you!
Source code is attached to this article.