Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Disable the Close box on a form

4.97/5 (27 votes)
18 Feb 2012CPOL 86.6K  
Disable the Close box on a form

If you want to prevent the user from closing your form until he has dealt with something really important, you can set

Form.ControlBox=false

but this also removes the Min, Max, and system menu from the form as well.
You cannot (it seems) remove just the Close box, but you can disable it, and it alone. To disable and grey out the Close box, override the CreateParams getter:

protected override CreateParams CreateParams
    {
        get
        {
            CreateParams parms = base.CreateParams;
            parms.ClassStyle |= 0x200;  // CS_NOCLOSE
            return parms;
        }
    }

WARNING:

  1. If you do this, all normal methods of closing the form become unavailable to the user. Use this only when it is really needed.
  2. Remember to provide some way to close the form (and test it!)

History

2016-07-20 Typo fixed - thanks Brisingr Aerowing!

2010-01-26 Original version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)