Click here to Skip to main content
15,878,945 members
Articles / Programming Languages / Visual Basic
Article

Immoveable Form

Rate me:
Please Sign up or sign in to vote.
2.80/5 (11 votes)
16 Mar 20053 min read 68.6K   800   19   15
How to create a form which cannot be repositioned by the end user.

Introduction

In Visual Basic 6, a form had a Moveable property. In the .NET Framework, this property no longer exists. This was possibly removed because restricting a user from moving a form is considered a bad UI design or maybe they just didn't get around to it. Regardless of whether it is a good idea to do something or not, I believe it should at least be possible.

Background

My personal, and potentially controversial, reason for needing this feature is that I like most applications on my PC to fill the screen. Unfortunately, too often the developer has coded the application to remember its size and position but not whether it was last maximized or not. As such I have developed the habit of resizing a window to fill the screen without maximizing it. This, maybe bad, habit has found its way into some of the software I have developed. I often size the main window of an application to fill the screen working area but unlike a maximized window, an accidental click and drag can move the window out of place and putting back is a hassle.

I use a .NET class library that I have developed with a class that inherits from System.Windows.Forms.Form and includes all the extra behaviour that I ever use in any application I build. I have extracted the code snippets responsible for implementing the Moveable property so that you can easily integrate it into any existing code you have without needing to inherit from my class.

Using the code

Whether you inherit from an extended form or you paste the code directly into a form with the rest of your code, it is very easy to use. Simply set the Moveable to False to disable both mouse dragging and the Move option on the system menu. Ideally you would set this in the Sub New method or in the Load event. You can set the property with the form designer too. The code is designed so that you can toggle the Moveable property at any point during run-time.

VB
Friend Class Form1
    Inherits ImmoveableForm
    Public Sub New()
        MyBase.New()
    
        'This call is required by the Windows Form Designer.
        InitializeComponent()
    
        'Add any initialization after the InitializeComponent() call
        
        'This disables the user's ability to move this form
        Me.Moveable = False
    End Sub
    'usual Visual Studio designer code here
End Class

Points of Interest

The resulting code for the ImmoveableForm is really quite simple as you can see in the source. Unfortunately for me, reaching this stage was not quite so straightforward. I have never written a Windows application in C/C++ or any language that requires accessing the Windows API directly so I spent a lot of time wading through the mass of API documentation provided by the MSDN Library. Once I had discovered that the only reliable way to achieve an immoveable form was to intercept window messages I was on my way to serious progress.

Disabling dragging the window caption was quite straightforward, as was ignoring selection of the Move item on the system menu. Actually preventing a selection of the Move menu item in the first place was interesting. My first thought was to delete the Move menu item from the system menu but the Windows standard tends to be greying and disabling menu items. Deleting also makes it difficult to restore the menu when the Moveable property changes from False to True without disturbing any other potential system menu changes made by this class or a subclass.

There were some problems with setting the Move menu item to disabled from within Moveable property handler. The call to disable the menu seemed to be completely ignored and a quick Google confirmed that I wasn't the only person having trouble with this. In the end I found that the answer was to trap the WM_INITMENUPOPUP window message for the system menu and make the call to disable the Move menu there. It took a while to find the documentation for the WM_INITMENUPOPUP message because it is oddly located under Keyboard Accelerators section in the Platform SDK documentation rather than the Menus section.

History

  • 16/03/2005
    • First release.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer Squixa
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
dj.3agl31-Sep-14 12:38
dj.3agl31-Sep-14 12:38 
GeneralAwesome! Pin
dj.3agl31-Sep-14 12:37
dj.3agl31-Sep-14 12:37 
QuestionDo we really need this while we have LocationChanged event. Pin
uspatel12-Apr-12 21:20
professionaluspatel12-Apr-12 21:20 
AnswerRe: Do we really need this while we have LocationChanged event. Pin
Member 42919067-Mar-17 1:30
Member 42919067-Mar-17 1:30 
Questionnot working for me Pin
noemailz10-Jan-07 4:06
noemailz10-Jan-07 4:06 
AnswerRe: not working for me Pin
noemailz10-Jan-07 4:17
noemailz10-Jan-07 4:17 
GeneralHelped me out big time! Pin
jgags18-Oct-06 5:17
jgags18-Oct-06 5:17 
Thank you. I could not find any documentation about a 'movable' method in .NET.
GeneralThis worked okay for me BUT.... Pin
Layston28-Aug-06 5:40
Layston28-Aug-06 5:40 
GeneralRe: This worked okay for me BUT.... Pin
Avanost25-Mar-07 3:47
Avanost25-Mar-07 3:47 
GeneralC# code Pin
syncter23-Oct-05 4:54
syncter23-Oct-05 4:54 
GeneralGreat..... Pin
Amol Srawde28-Jun-05 19:01
Amol Srawde28-Jun-05 19:01 
GeneralForcing users... Pin
StealthyMark16-Mar-05 0:42
StealthyMark16-Mar-05 0:42 
GeneralRe: Forcing users... Pin
Ashaman16-Mar-05 1:44
Ashaman16-Mar-05 1:44 
GeneralRe: Forcing users... Pin
Jason Stangroome16-Mar-05 11:13
professionalJason Stangroome16-Mar-05 11:13 
GeneralRe: Forcing users... Pin
Ashaman17-Mar-05 6:55
Ashaman17-Mar-05 6:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.