Click here to Skip to main content
15,883,558 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Disable the Close box on a form

Rate me:
Please Sign up or sign in to vote.
4.97/5 (27 votes)
18 Feb 2012CPOL 84.2K   15   14
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)


Written By
CEO
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

 
PraiseGreat snippet Pin
Member 1570357011-Jul-22 13:05
Member 1570357011-Jul-22 13:05 
GeneralRe: Great snippet Pin
OriginalGriff11-Jul-22 18:51
mveOriginalGriff11-Jul-22 18:51 
BugTypo. Pin
Brisingr Aerowing19-Jul-16 13:50
professionalBrisingr Aerowing19-Jul-16 13:50 
GeneralRe: Typo. Pin
OriginalGriff19-Jul-16 22:42
mveOriginalGriff19-Jul-16 22:42 
GeneralRe: Typo. Pin
Brisingr Aerowing20-Jul-16 4:02
professionalBrisingr Aerowing20-Jul-16 4:02 
GeneralMy vote of 4 Pin
Sanjay K. Gupta1-Jun-12 18:34
professionalSanjay K. Gupta1-Jun-12 18:34 
QuestionNice tip Pin
BillW333-May-12 3:04
professionalBillW333-May-12 3:04 
GeneralReason for my vote of 5 I found it helpful. Hence voted 5. Pin
ProEnggSoft24-Feb-12 18:50
ProEnggSoft24-Feb-12 18:50 
GeneralThis is very helpfull. Thank you very much xD // Enable/Dis... Pin
Ben Pretorius4-Jul-11 22:41
Ben Pretorius4-Jul-11 22:41 
GeneralReason for my vote of 5 I liked it. Found it helpful and wor... Pin
All Time Programming4-Jul-11 20:50
All Time Programming4-Jul-11 20:50 
GeneralReason for my vote of 5 Good one i have used before works we... Pin
charles henington27-Jun-11 12:01
charles henington27-Jun-11 12:01 
GeneralHow to alternatively close the form after this? Or How to en... Pin
maxxon1517-Apr-11 5:33
maxxon1517-Apr-11 5:33 
GeneralRe: You can't AFAIK re-enable the button (the CreateParms getter... Pin
OriginalGriff17-Apr-11 21:09
mveOriginalGriff17-Apr-11 21:09 
GeneralRe: You can't AFAIK re-enable the button (the CreateParms getter... Pin
BillW333-May-12 3:03
professionalBillW333-May-12 3:03 
Too bad they didn't provide a way to re-enable the Close box, but adding Close button will do the job just fine. Smile | :)
Just because the code works, it doesn't mean that it is good code.

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.