Dragging - moving controls
Posted: Thu Aug 20, 2009 1:16 am
Hello,
This little snip will show you how to drag and move a control around your form using the mouse.
Here is the code:
Make public variables called:
Public capt = 0
Public a
Public b
Place the following codes in your chosen controls mouse events:
MouseUp
Replace the word 'Yourcontrol' with the name of your control. So for example if I wanted to drag and move and picturebox control the code would look like this:

This little snip will show you how to drag and move a control around your form using the mouse.
Here is the code:
Make public variables called:
Public capt = 0
Public a
Public b
Place the following codes in your chosen controls mouse events:
MouseUp
Code: Select all
MouseDown
YourControl.Location = New Point(YourControl.Location.X + (e.X - a), YourControl.Location.Y + (e.Y - b))
capt = 0
Code: Select all
MouseMove
a = e.X
b = e.Y
capt = 1
Code: Select all
If capt = 1 Then
YourControl.Location = New Point(YourControl.Location.X + (e.X - a), YourControl.Location.Y + (e.Y - b))
End If
Replace the word 'Yourcontrol' with the name of your control. So for example if I wanted to drag and move and picturebox control the code would look like this:
Code: Select all
Happy dragging Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
PictureBox1.Location = New Point(PictureBox1.Location.X + (e.X - a), PictureBox1.Location.Y + (e.Y - b))
capt = 0
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
a = e.X
b = e.Y
capt = 1
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If capt = 1 Then
PictureBox1.Location = New Point(PictureBox1.Location.X + (e.X - a), PictureBox1.Location.Y + (e.Y - b))
End If
End Sub
