School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 306,725 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,589 people online right now. Registration is fast and FREE... Join Now!




Moving a Borderless Form

 
Reply to this topicStart new topic

> Moving a Borderless Form

Sethro117
Group Icon



post 31 Oct, 2009 - 06:58 PM
Post #1


Ive seen this topic quite a bit in the VB.NET Forums; people wanting to move Borderless Forms and such. So here's a tutorial to simply show you how easy it is.

First, we need to declare our variables:
CODE
    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:
CODE
    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:
CODE
    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:
CODE
    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?
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

AdamSpeight2008
Group Icon



post 1 Nov, 2009 - 08:05 AM
Post #2
Good Tutorial.

The form can be dragged with any mouse key press, personally I prefer for the dragging to happen with a left button which gices the possibility of canceling the dragging with the right.
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/20/09 02:07PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month