2 Replies - 6519 Views - Last Post: 27 July 2012 - 04:26 PM Rate Topic: -----

#1 Alyssa Saila   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 07-January 12

Moving Object's XY Position With Mouse Cursor

Posted 27 July 2012 - 07:29 AM

What I am attempting to do is move any control (form, panel, button anything) with the mouse cursor.

Using the function calls...

MoveWithCursor(form1);
MoveWithCursor(form2);
MoveWithCursor(panel1);
MoveWithCursor(anyOtherKindOfObject);
MoveWithCursor(etc);




I am wanting the ability to move any object with my cursor (that I call with these functions).
Basically I'm hoping for a function call that would move objects with my cursor.
The more function calls, the more objects I can have access to -to move with my cursor.
Note that I understand that some controls will only be visible if moved only within the bounds of their parent.
However, I still would like them to move around if selected with the cursor (even if it only moves around it's parent).
For any form of course, I would like the forms to move anywhere within the bounds of the parent screen.

As of right now this only works with one object (one function call at a time).

If I call this function back to back attempting to move form1 & form2..
...or if I call this function back to back attempting move form2 & panel1 etc..
...then it freezes up on me, and I've tried a lot of different things to no avail.

Any ideas or help with my code to achieve this?

private static int FirstClickDownPosX { get; set; }
private static int FirstClickDownPosY { get; set; }
private static int MouseDragToPosX { get; set; }
private static int MouseDragToPosY { get; set; }
private static Control RepositionableObject { get; set; }

private void MoveWithCursor_MouseDown(object sender, MouseEventArgs e)
{
	FirstClickDownPosX = e.X;
	FirstClickDownPosY = e.Y;
}
private void MoveWithCursor_MouseMove(object sender, MouseEventArgs e)
{
	MouseDragToPosX = e.X;
	MouseDragToPosY = e.Y;

	if (e.Button == MouseButtons.Left)
	{
		RepositionableObject.Left += (MouseDragToPosX - FirstClickDownPosX);
		RepositionableObject.Top += (MouseDragToPosY - FirstClickDownPosY);
	}
}
internal void MoveWithCursor(Control newObject)
{
	RepositionableObject = newObject;
	RepositionableObject.MouseDown += new MouseEventHandler(MoveWithCursor_MouseDown);
	RepositionableObject.MouseMove += new MouseEventHandler(MoveWithCursor_MouseMove);
}



Is This A Good Question/Topic? 0
  • +

Replies To: Moving Object's XY Position With Mouse Cursor

#2 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Moving Object's XY Position With Mouse Cursor

Posted 27 July 2012 - 07:57 AM

I suspect it has to do with your adding of the events in lines 26 and 27. You never remove these events when you are done moving the object.
Was This Post Helpful? 1
  • +
  • -

#3 Alyssa Saila   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 07-January 12

Re: Moving Object's XY Position With Mouse Cursor

Posted 27 July 2012 - 04:26 PM

I got it working in case anyone cares (thanks again Momerath).
I added "RepositionableObject = ((Control)sender);" above the part where I capture the first click down coordinates.

RepositionableObject = ((Control)sender);  // I added this line
FirstClickDownPosX = e.X;
FirstClickDownPosY = e.Y;



I tested it successfully with the calls...
MoveWithCursor(this);
MoveWithCursor(form2);
MoveWithCursor(panel1);



After the calls they all clicked dragged with the cursor.
What I like about this is I can move the panel1 control around on the main form, and then if I want I can move the main form1 and panel1 stays in it's relative position.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1