# VBForums CodeBank > CodeBank - Visual Basic .NET >  Moving a Borderless form, simple & short.

## noahssite

First of all credits go to CoderJoe though he isn't active.

Here is the code:


vb Code:
Public Class Form1
    'Declare the variables
    Dim drag As Boolean
    Dim mousex As Integer
    Dim mousey As Integer
     Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        drag = True 'Sets the variable drag to true.
        mousex = Windows.Forms.Cursor.Position.X - Me.Left 'Sets variable mousex
        mousey = Windows.Forms.Cursor.Position.Y - Me.Top 'Sets variable mousey
    End Sub
     Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        'If drag is set to true then move the form accordingly.
        If drag Then
            Me.Top = Windows.Forms.Cursor.Position.Y - mousey
            Me.Left = Windows.Forms.Cursor.Position.X - mousex
        End If
    End Sub
     Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
        drag = False 'Sets drag to false, so the form does not move according to the code in MouseMove
    End Sub
End Class

You can place the code on the events of any control. Say if you would like to use an image as a title bar, then just place the code on the appropiate events of the image.

----------


## manavo11

In VB6 you could do this:


vb Code:
Option Explicit Private Declare Function ReleaseCapture Lib "user32" () As LongPrivate Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Const HTCAPTION = 2Private Const WM_NCLBUTTONDOWN = &HA1 Private Const WM_SYSCOMMAND = &H112Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)    ReleaseCapture    SendMessage hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&End Sub

I assume if you fix the API declaration, it would work as well?

----------


## noahssite

Well im not good with APIs and i only know .NET though:

The way i supplied is very simple, short & easy (like my title).
Also the code i supplied works completly fine, no flaus. Unless you can find any.

----------


## manavo11

I just thought I'd mention another method. Have all the methods put together in one thread  :Smilie:

----------


## noahssite

Well is the API method better?
I also added your code to my first post.

----------


## manavo11

Actually the code is VB6, so the declarations at least will need a small conversion, as well as the function declaration of the Picture1_MouseDown  :Smilie: 

Can't really say if one is better than the other. Both work, so after that it's just a matter of what you prefer I suppose...

----------


## stanav

Using plain .net code (as Noahssite's example) can give flickers and lags especially if the form contains images and/or a lot of controls while using API will produce a much smoother move of the form.

----------


## dekz

Im really new to this, im trying to find codes so that i can implement the features i want in the forms, nothing makes sense to me, i copy the codes people post, like this one, all turns out with errors, this one i get *Event MouseDown cannot be found*, and this one #
        If drag Then
#
            Me.Top = Windows.Forms.Cursor.Position.Y - mousey
#
            Me.Left = Windows.Forms.Cursor.Position.X - mousex 

*Expression is a value and therefore cannot be the target of an assignement.*



I dont know what it is about this coding but it seriously pisses me off, it should be easy, i dont understand why i cant complete a simple form without going into some stupid coding to tell it what to do, allready most of the parts about the window is there in a graphical UI, so why isnt the rest, if this language was so smart, how come it cant be made simpler. you basically type one thing wrong and you screw up the whole project lol

----------


## minitech

Actually, overriding WndProc is the best way...

vb Code:
Protected Overrides Sub WndProc(ByRef e As Message)      MyBase.WndProc(e)      If e.Msg = &H84 AndAlso e.Result.ToInt32() = 1 Then e.Result = New IntPtr(2)End Sub

EDIT: Sorry, it's ByRef.

----------


## dekz

what do i do if i have a Private Sub under the form and want the Private Sub to be moveable?

----------


## noahssite

What do you mean you want the Private Sub to be moveable?

----------


## dekz

> What do you mean you want the Private Sub to be moveable?


I figured it out, i had problem where to put the code with a control panel on top of form1.

----------


## Legjendat

If I put the code on PictureBoxX_MouseHover, then it will only move to the right and down, it won't go up and left. any way to fix that cuz like this it only works on the main form area. Thanks anyway, proves useful.

----------


## noahssite

> If I put the code on PictureBoxX_MouseHover, then it will only move to the right and down, it won't go up and left. any way to fix that cuz like this it only works on the main form area. Thanks anyway, proves useful.


Post your code.

----------


## Legjendat

The same code as in the thread opening post, that is.




> Dim drag As Boolean
> 
>     Dim mousex As Integer
> 
>     Dim mousey As Integer
> 
> 
>  Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
> 
> ...

----------


## noahssite

No the code that failed to work...

You said



> If I put the code on PictureBoxX_MouseHover,


How did you use Picturebox_MouseHover? Did you use it instead of Form_MouseMove? Post THAT code.

----------


## Legjendat

> Private Sub PictureBox5_MouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox5.MouseHover
> 
>         drag = True
> 
>         mousex = Windows.Forms.Cursor.Position.X - Me.Left
> 
>         mousey = Windows.Forms.Cursor.Position.Y - Me.Top
> 
>         If drag Then
> ...



Actually, I was mistaken. it won't move at all, i guess the reason it moved, is because i hadn't deleted ur code. so till the form1_mousemove was there it would move as I explained, now that I deleted the code in form1_mousemove it won't move at all. same thing happens in picturebox_click

EDIT:

your last comment finally made me understand where my mistake stood, i wasn't putting the code under the correct properties. I finally made it thanks for your comment again, it made me understand.

----------

