Sometimes, you need to make the control draggable during runtime. It can be a form with
FormBorderStyle property set to
None, or any other situation.
I want to propose a simple extension method that allows you to make this possible.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace DraggableControls
{
public static class ControlExtension
{
private static Dictionary<Control, bool> draggables =
new Dictionary<Control, bool>();
private static System.Drawing.Size mouseOffset;
public static void Draggable(this Control control, bool Enable)
{
if (Enable)
{
if (draggables.ContainsKey(control))
{ return;
}
draggables.Add(control, false);
control.MouseDown += new MouseEventHandler(control_MouseDown);
control.MouseUp += new MouseEventHandler(control_MouseUp);
control.MouseMove += new MouseEventHandler(control_MouseMove);
}
else
{
if (!draggables.ContainsKey(control))
{ return;
}
control.MouseDown -= control_MouseDown;
control.MouseUp -= control_MouseUp;
control.MouseMove -= control_MouseMove;
draggables.Remove(control);
}
}
static void control_MouseDown(object sender, MouseEventArgs e)
{
mouseOffset = new System.Drawing.Size(e.Location);
draggables[(Control)sender] = true;
}
static void control_MouseUp(object sender, MouseEventArgs e)
{
draggables[(Control)sender] = false;
}
static void control_MouseMove(object sender, MouseEventArgs e)
{
if (draggables[(Control)sender] == true)
{
System.Drawing.Point newLocationOffset = e.Location - mouseOffset;
((Control)sender).Left += newLocationOffset.X;
((Control)sender).Top += newLocationOffset.Y;
}
}
}
}
It's pretty simple to use this code. To make control draggable, you just need to write one line of code:
AnyControl.Draggable(true);
and to disable this feature:
AnyControl.Draggable(false);
For example, if your Form contains two checkboxes named
checkBox_DragForm and
checkBox_DragButton with
CheckedChanged event handlers assigned, and button with name
btnTest, you may use the following code to enable/disable drag feature on Form (represented by
this) or/and button:
private void checkBox_DragForm_CheckedChanged(object sender, EventArgs e)
{
this.Draggable(checkBox_DragForm.Checked);
}
private void checkBox_DragButton_CheckedChanged(object sender, EventArgs e)
{
btnTest.Draggable(checkBox_DragButton.Checked);
}
The code sample is
here (VS2010 solution).
Hope this would be helpful.