Monday, 8 March 2010

VB.Net - Stopping a form from being moved

I have just managed to finish what I thought would be a 5 minute task that ended up taking me a lot longer than that.

I have a VB.Net application and a form that the client doesn't want the user to be able to move (it needs to hide data that the user validates).

I found various suggestions on-line. The most common being to remove the border of the form. This does work but looks awful so I carried on looking.

I eventually found several code samples written in C# and managed to convert the simplest looking one to VB.Net. I pasted it in the code page for the form and it worked. So here it is....

Protected Overloads Overrides Sub WndProc(ByRef m As Message)
Const WM_NCLBUTTONDOWN As Integer = 161
Const WM_SYSCOMMAND As Integer = 274
Const HTCAPTION As Integer = 2
Const SC_MOVE As Integer = 61456

If (m.Msg = WM_SYSCOMMAND) AndAlso (m.WParam.ToInt32() = SC_MOVE) Then
Exit Sub
End If

If (m.Msg = WM_NCLBUTTONDOWN) AndAlso (m.WParam.ToInt32() = HTCAPTION) Then
Exit Sub
End If

MyBase.WndProc(m)
End Sub

No comments:

Post a Comment