Click here to Skip to main content
15,889,693 members
Articles / Programming Languages / Visual Basic 6
Alternative
Tip/Trick

Very simple way to move a form holding down mouse button

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
16 Mar 2011CPOL 7.4K   1   1
This method was converted to VB from C# but I believe to be much easier, althought it does not allow the F5 Key as yours does. Private panel1 As New Panel() Private canMove As Boolean Private currentPosition As Point Public Sub New() ...
This method was converted to VB from C# but I believe to be much easier, althought it does not allow the F5 Key as yours does.

Private panel1 As New Panel()
Private canMove As Boolean
Private currentPosition As Point
Public Sub New()
    Me.Controls.Add(panel1)
    panel1.Dock = DockStyle.Top
    panel1.BackColor = Color.Black
    Me.FormBorderStyle = FormBorderStyle.None
    AddHandler panel1.MouseDown, AddressOf panel1_MouseDown
    AddHandler panel1.MouseUp, AddressOf panel1_MouseUp
    AddHandler panel1.MouseMove, AddressOf panel1_MouseMove
End Sub

Private Sub panel1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
    If canMove Then
        Dim newPosition As Point = Control.MousePosition
        newPosition.X = newPosition.X - currentPosition.X
        newPosition.Y = newPosition.Y - currentPosition.Y
        Me.Location = newPosition
    End If
End Sub

Private Sub panel1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
    canMove = False
End Sub

Private Sub panel1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
    canMove = True
    currentPosition.X = e.X
    currentPosition.Y = e.Y
End Sub

License

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


Written By
United States United States
I do not claim to be wrong! I just rarely ever write.

Comments and Discussions

 
GeneralReason for my vote of 5 nice Pin
cSNewb22-Apr-11 6:02
cSNewb22-Apr-11 6:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.