Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to create a form on which user can drag the control(pictureBox)from one side to another side with the mouse...
I searched but I couldn't find the solution...
My problem is how can I get the mouse position(X & Y)on the form(when the mouse is on the control)???

Thanks in Advance...

Regards:
Jayanta..
Posted
Updated 19-Aug-13 4:04am
v2

Then you really didn't search that hard. I really hate self-promotion, but here: Create your Own Runtime Movable Windows Forms Controls[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Aug-13 10:38am    
There is nothing wrong in this kind of "self-promotion"; it can be quite helpful. My 5.
—SA
Maciej Los 19-Aug-13 11:54am    
Agree
+5!
Sergey Alexandrovich Kryukov 19-Aug-13 12:20pm    
Being really helpful is all what matters here...
—SA
JayantaChatterjee 19-Aug-13 10:42am    
Your links program is in VB.NET(which I didn't know)...and I also download the source File which gives an error("Type 'DskResearch.RuntimeMovableControls.MovableLabel' is not defined").
Dave Kreskowiak 19-Aug-13 14:47pm    
Easily translated in any online conversion tool. Also, you know more VB.NET than you know.

You have to compile both projects in the solution, not just the test project.
#region move picture at runtime
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
isDragging = true;

currentX = e.X;
currentY = e.Y;
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
pictureBox1.Top = pictureBox1.Top + (e.Y - currentY);
pictureBox1.Left = pictureBox1.Left + (e.X - currentX);
}
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
#endregion
 
Share this answer
 
Comments
Philippe Mori 11-Jun-15 22:44pm    
Well, if you move the picture box immediatly, then you also have to update currentX and currentY each time otherwise move will get larger as you move away from original position.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900