Drag and Drop Windows Forms Button

I am creating a new Windows Forms application. I drag the button into the form. I need to drag this button to another place on this form at runtime. any code snippets or links are appreciated.

I spent half an hour searching before coming here.

+4
source share
1 answer

You can start with something like this:

bool isDragged = false; Point ptOffset; private void button1_MouseDown( object sender, MouseEventArgs e ) { if ( e.Button == MouseButtons.Left ) { isDragged = true; Point ptStartPosition = button1.PointToScreen(new Point(eX, eY)); ptOffset = new Point(); ptOffset.X = button1.Location.X - ptStartPosition.X; ptOffset.Y = button1.Location.Y - ptStartPosition.Y; } else { isDragged = false; } } private void button1_MouseMove( object sender, MouseEventArgs e ) { if ( isDragged ) { Point newPoint = button1.PointToScreen(new Point(eX, eY)); newPoint.Offset(ptOffset); button1.Location = newPoint; } } private void button1_MouseUp( object sender, MouseEventArgs e ) { isDragged = false; } 
+10
source

All Articles