Monday, 30 September 2013

Grab, move and drop control in Form by mouse

Grab, move and drop control in Form by mouse

Hello I have found this code that might help me with following issue, I'm
trying to make drag, drop and move label in my Form by mouse.
private Point MouseDownLocation;
private void MyControl_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}
private void MyControl_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
this.Left = e.X + this.Left - MouseDownLocation.X;
this.Top = e.Y + this.Top - MouseDownLocation.Y;
}
}
But when I assing mousemove and mousedown as events to label and i try to
grab the label and move with mouse it moves with the whole Form.
May I ask where is should the code be improved?
Thank you for your time.

No comments:

Post a Comment