Move a .NET Form that has no title bar

If you have an Irregular form (one that is not the basic rectangular shape) it probably doesn't have a title bar which means the user cannot move it. This code allows you to hold the left mouse button down and drag the form to a new location.

Add these module level variables:

    Private myPointClicked As Point
    Private myIrregular As New clsIrregular()

Add this code:

    Private Sub form1_MouseDown(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown

        myFormDragging = True
        myPointClicked = New Point(e.X, e.Y)
    End Sub

    Private Sub form1_MouseUp(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp

        myFormDragging = False
    End Sub

    Private Sub form1_MouseMove(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove

        If myFormDragging Then
            Dim aMoveToPoint As Point
            '
            ' Use the current mouse position to find the target location.
            '
            aMoveToPoint = Me.PointToScreen(New Point(e.X, e.Y))
            '
            ' Adjust the position based on where you started.
            '
            aMoveToPoint.Offset(myPointClicked.X * -1, _
                (myPointClicked.Y + SystemInformation.CaptionHeight + _
               SystemInformation.BorderSize.Height) * -1)
            '
            ' Move the form.
            '
            Me.Location = aMoveToPoint
        End If
    End Sub




About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2024 TheScarms
Goto top of page