Search code examples
vb.netpanel

move form when panel is clicked vb.net


I have a form1 with none border-style & I want to make this form moving when the user moves panel1. after making the simple search is stack over I found the same case with this solution C#

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
    this.Location = new Point(Cursor.Position.X + e.X , Cursor.Position.Y + e.Y);
}
}

for my case I use vb.net, so I try to make similar code but I failed :

Private Sub Panel1_MouseMove(sender As Object, e As MouseEventArgs) Handles GunaPanel1.MouseMove
    If e.Button = MouseButtons.Left Then
        MainDashboard.Location = New Point(Cursor.Position.X + e.X, Cursor.Position.Y + e.Y)
    End If
End Sub

Solution

  • Here is solution : MouseIsDown and MouseIsDownLoc is module level variables.

        Private MouseIsDown As Boolean = False
        Private MouseIsDownLoc As Point = Nothing
        Private Sub Panel1_MouseMove(sender As Object, e As MouseEventArgs) Handles Panel1.MouseMove
    
            If e.Button = MouseButtons.Left Then
                If MouseIsDown = False Then
                    MouseIsDown = True
                    MouseIsDownLoc = New Point(e.X, e.Y)
                End If
    
                Me.Location = New Point(Me.Location.X + e.X - MouseIsDownLoc.X, Me.Location.Y + e.Y - MouseIsDownLoc.Y)
            End If
        End Sub
        Private Sub Panel1_MouseUp(sender As Object, e As MouseEventArgs) Handles Panel1.MouseUp
            MouseIsDown = False
        End Sub