Click here to Skip to main content
15,170,505 members
Articles / Programming Languages / C#
Posted 7 Sep 2007


88 bookmarked

Disabling Close Button on Forms

Rate me:
Please Sign up or sign in to vote.
4.80/5 (68 votes)
12 Sep 2007CPOL2 min read
How to disable the Close button on C# WinForms
Screenshot - TestForm.gif


To prevent the user from closing the form during data processing, it would be good if we disable the Close button on the form. Whenever it is required to show such a form with the Close button disabled, the first step is to look into the properties of the form to find the corresponding property. But I have found that form does not have such a kind of property provided by VS.NET/C#. Hence we need to do it programmatically and this article presents how to do it.


In one of my projects, I had to implement a form with Close button disabled, so that the user cannot leave the form until it finishes the data processing. From the form designer window in VS.NET 2005, it is possible to hide the Minimize box and Maximize box. But there is no property called Close or Show close. Then I had some discussions with the team mates and got a couple of ways to do this. Among those alternatives, finally my idea got the nod. I thought of sharing this idea with The Code Project community and hence I have written this small article.

Using the Code

During construction and creation of the Form object, .NET would use the default creation parameters available in the base class CreateParams property. In fact, CreateParams property is available in Forms.Control class. In our form class (derived from System.Windows.Forms.Form), override this property and modify the creation flags. For disabling the Close button use 0x200 to modify the ClassStyle member of the CreateParams.

// source code 
// Code Snippet
 private const int CP_NOCLOSE_BUTTON = 0x200;
 protected override CreateParams CreateParams
        CreateParams myCp = base.CreateParams;
        myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON ;
        return myCp;

That's it! We are done with the coding.

Points of Interest

The trick here is to override the CreateParams property in our Form with modified create flags. Directly copy the above piece of code and paste it to your Form class and it should work. Happy coding!!!


  • 7th September, 2007: Initial version created


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


About the Author

Giri Ganji
Web Developer
India India
No Biography provided

Comments and Discussions

QuestionError Pin
Member 1263742626-Nov-16 6:41
MemberMember 1263742626-Nov-16 6:41 
QuestionThanks a lot Pin
Member 267444231-Jul-16 19:22
MemberMember 267444231-Jul-16 19:22 
QuestionGood Work Pin
VDKK24-Mar-16 2:18
MemberVDKK24-Mar-16 2:18 
QuestionBut I can go to the taskbar and choose "Close Window" Pin
Nighty666624-Sep-15 5:56
MemberNighty666624-Sep-15 5:56 
QuestionThank you! Pin
K K Allentown1-Oct-14 6:42
MemberK K Allentown1-Oct-14 6:42 
GeneralExcellent Pin
Francesco Giossi18-Jun-14 0:24
MemberFrancesco Giossi18-Jun-14 0:24 
GeneralRe: Excellent Pin
spencepk30-Jul-14 5:19
Memberspencepk30-Jul-14 5:19 
QuestionNice piece of code Pin
sysmaniax13-Nov-13 0:00
Membersysmaniax13-Nov-13 0:00 
GeneralWorks great ! Pin
TransformYa19-Aug-13 1:08
professionalTransformYa19-Aug-13 1:08 
GeneralMy vote of 5 Pin
Member 977323923-Apr-13 0:08
MemberMember 977323923-Apr-13 0:08 
QuestionI vote 5 Pin
Moxxis29-Mar-13 11:41
MemberMoxxis29-Mar-13 11:41 
GeneralMy vote of 5 Pin
HowitZer2628-Mar-13 10:43
MemberHowitZer2628-Mar-13 10:43 
QuestionOknevermind Pin
Branimir4427-Mar-13 22:08
MemberBranimir4427-Mar-13 22:08 
GeneralMy vote of 5 Pin
Mahyar.FF22-Dec-12 3:02
MemberMahyar.FF22-Dec-12 3:02 
GeneralMy vote of 5 Pin
supernatik12-Dec-12 23:09
Membersupernatik12-Dec-12 23:09 
GeneralMy vote of 5 Pin
Andrewpeter9-Sep-12 23:40
MemberAndrewpeter9-Sep-12 23:40 
Questionakhilesh Pin
Member 928654531-Aug-12 20:23
MemberMember 928654531-Aug-12 20:23 
GeneralMy vote of 4 Pin
Itz.Irshad3-Apr-12 19:35
MemberItz.Irshad3-Apr-12 19:35 
GeneralMy vote of 5 Pin
kharlos5-Sep-11 7:35
Memberkharlos5-Sep-11 7:35 
Questionnot bad Pin
BillW3315-Jul-11 8:22
professionalBillW3315-Jul-11 8:22 
GeneralHow to re-enable it? Pin
maxxon1517-Apr-11 6:46
Membermaxxon1517-Apr-11 6:46 
GeneralMy vote of 5 Pin
JunfengGuo10-Apr-11 17:24
MemberJunfengGuo10-Apr-11 17:24 
GeneralMy vote of 1 Pin
Sharjith27-Mar-11 17:46
professionalSharjith27-Mar-11 17:46 
GeneralRe: My vote of 1 Pin
HowitZer2628-Mar-13 10:46
MemberHowitZer2628-Mar-13 10:46 
GeneralRe: My vote of 1 Pin
Behzad Sedighzadeh26-Mar-14 0:49
MemberBehzad Sedighzadeh26-Mar-14 0:49 

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.