Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB6

Very simple way to move a form holding down mouse button

5.00/5 (3 votes)
16 Mar 2011CPOL 5.7K  
Charles henington, you can consider this as VB.NET.F5 is nothing but start debugging. You got my 5.Public Class Form1 Dim pastx, pasty, presentx, presenty, bt As Integer Sub moving() Dim xx, yy As Integer xx = presentx - pastx yy = presenty - pasty ...
Charles henington, you can consider this as VB.NET.
F5 is nothing but start debugging. You got my 5.

VB
Public Class Form1
    Dim pastx, pasty, presentx, presenty, bt As Integer

    Sub moving()
        Dim xx, yy As Integer
        xx = presentx - pastx
        yy = presenty - pasty
        If bt Then 'set your condition: left btn or right btn or mid btn
            Me.Left = Me.Left + xx
            Me.Top = Me.Top + yy
        End If
    End Sub

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        pastx = e.X
        pasty = e.Y
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        presentx = e.X
        presenty = e.Y
        bt = e.Button
        moving()
    End Sub
End Class

License

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