First, we need to declare our variables:
Dim drag As Boolean Dim mousex As Integer Dim mousey As Integer
Next we need to tell the application its time to drag by setting drag to true and then letting the form know which way or location to head by getting the position of our Cursor:
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown drag = True mousex = Windows.Forms.Cursor.Position.X - Me.Left mousey = Windows.Forms.Cursor.Position.Y - Me.Top End Sub
The code below actually does the moving of the Form for us. Since we clicked on the Form, drag was set to true so now as we move our mouse the Form will follow:
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove If drag Then Me.Top = Windows.Forms.Cursor.Position.Y - mousey Me.Left = Windows.Forms.Cursor.Position.X - mousex End If End Sub
Finally, its time to stop dragging the form so as we release our mouse from the Form, drag needs to be set to false so it wont move anywhere until its clicked again:
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp drag = False End Sub
Pretty simple, no?





MultiQuote






|