954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

How to make your controls moveable?

By Ramy Mahrous on Jul 11th, 2009 2:14 am

Here's I wrote some code to move controls on the form
1- Assign (Control_MouseMove, Control_MouseDown, and Control_MouseUp) to the controls you need it movable.

//This code developed by Ramy Mahrous 
//ramyamahrous@hotmail.com
//Its contents is provided "as is", without warranty.

/// <summary>
/// Indicates whether control is pressed by mouse or not
/// </summary>
bool IsDraged = false;

/// <summary>
/// Holds the X-coordinate of the control
/// </summary>
int Control_X = 0;

/// <summary>
/// Holds the Y-coordinate of the control
/// </summary>
int Control_Y = 0;

private void Control_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
IsDraged = true;
Control_X = e.X;
Control_Y = e.Y;
}
}

private void Control_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
IsDraged = false;
}
}

private void Control_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Control active = (Control)sender;
if (IsDraged)
{
active.Left += e.X - Control_X / 12;
active.Top += e.Y - Control_Y / 12;
Control_X = e.X;
Control_Y = e.Y;
}
}
}

thanks dear friend

persianc#
Newbie Poster
1 post since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

Here's an even simpler example. You did unnecessary steps that can be avoided, even though yours is a good example. :)

Note: "Control" is the name of the object you want to move, so link it up in the Designer area first.

private Point pointMouse = new Point();
private Control ctrlMoved = new Control();
private bool bMoving = false;

private void Control_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
	//if not left mouse button, exit
	if (e.Button != MouseButtons.Left)
	{
		return;
	}
	// save cursor location
	pointMouse = e.Location;
	//remember that we're moving
	bMoving = true;
}

private void Control_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
	bMoving = false;
}

private void Control_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
	//if not being moved or left mouse button not used, exit
	if (!bMoving || e.Button != MouseButtons.Left)
	{
		return;
	}
	//get control reference
	ctrlMoved = (Control)sender;
	//set control's position based upon mouse's position change
	ctrlMoved.Left += e.X - pointMouse.X;
	ctrlMoved.Top += e.Y - pointMouse.Y;
}
cipher_nemo
Newbie Poster
1 post since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

Hi..
May I ask if how can I move two controls simultaneously. I have 3 instances of picturebox and 3 corresponding label at each bottom of picturebox. Picturebox are plotted on the panel inside user control. Now, I want to move specific picturebox, but what I want is that label will follow to the picturebox and place itself under picturebox. Is that possible? Please supply some codes. Thanks. :idea:

vigor
Newbie Poster
3 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

First solution came is to group them inside UserControl, and play with them like above.

Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 

But how can I group them? Will 'Struct' and 'Dictionary' applicable in that case? Please provide some codes.:-/:confused:

vigor
Newbie Poster
3 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

Here's an even simpler example. You did unnecessary steps that can be avoided, even though yours is a good example. :)

Note: "Control" is the name of the object you want to move, so link it up in the Designer area first.

private Point pointMouse = new Point();
private Control ctrlMoved = new Control();
private bool bMoving = false;

private void Control_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
	//if not left mouse button, exit
	if (e.Button != MouseButtons.Left)
	{
		return;
	}
	// save cursor location
	pointMouse = e.Location;
	//remember that we're moving
	bMoving = true;
}

private void Control_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
	bMoving = false;
}

private void Control_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
	//if not being moved or left mouse button not used, exit
	if (!bMoving || e.Button != MouseButtons.Left)
	{
		return;
	}
	//get control reference
	ctrlMoved = (Control)sender;
	//set control's position based upon mouse's position change
	ctrlMoved.Left += e.X - pointMouse.X;
	ctrlMoved.Top += e.Y - pointMouse.Y;
}

This is practically the same code as the OP, only you used a point instead of two ints (which is a good idea but to each their own)

I would like to point out when you declared the ctrlMoved variable, you set it to a "new control" since you are using that variable to hold reference to existing controls, That is unnecessary and you are creating an orphaned object. Same thing with the pointMouse variable. You immediately assign it to reference e.location so the "new point" is never used, its just orphaned for the garbage collector to pick up.

Either way, as always, thanks to both of you for sharing code, its always nice to see additions to the snippet library.

Diamonddrake
Master Poster
724 posts since Mar 2008
Reputation Points: 442
Solved Threads: 89
 

How about lines? Are they too movable and How?

vigor
Newbie Poster
3 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You