Click here to Skip to main content
Click here to Skip to main content
Go to top

Disabling Close Button on Forms

, 12 Sep 2007
Rate this:
Please Sign up or sign in to vote.
How to disable the Close button on C# WinForms
Screenshot - TestForm.gif

Introduction

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.

Background

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
 {
     get
     {
        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!!!

History

  • 7th September, 2007: Initial version created

License

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

Share

About the Author

Giri Ganji
Web Developer
India India
No Biography provided

Comments and Discussions

 
QuestionThank you! PinmemberK K Allentown2hrs 24mins ago 
GeneralExcellent PinmemberFrancesco Giossi17-Jun-14 23:24 
GeneralRe: Excellent Pinmemberspencepk30-Jul-14 4:19 
QuestionNice piece of code Pinmembersysmaniax12-Nov-13 23:00 
GeneralWorks great ! PinprofessionalTransformYa19-Aug-13 0:08 
GeneralMy vote of 5 PinmemberMember 977323922-Apr-13 23:08 
QuestionI vote 5 PinmemberMoxxis29-Mar-13 10:41 
Nicely done.
GeneralMy vote of 5 PinmemberHowitZer2628-Mar-13 9:43 
QuestionOknevermind PinmemberBranimir4427-Mar-13 21:08 
GeneralMy vote of 5 PinmemberMahyar.FF22-Dec-12 2:02 
GeneralMy vote of 5 Pinmembersupernatik12-Dec-12 22:09 
GeneralMy vote of 5 PinmemberAndrewpeter9-Sep-12 22:40 
Questionakhilesh PinmemberMember 928654531-Aug-12 19:23 
GeneralMy vote of 4 PinmemberItz.Irshad3-Apr-12 18:35 
GeneralMy vote of 5 Pinmemberkharlos5-Sep-11 6:35 
Questionnot bad [modified] PinmemberCIDev15-Jul-11 7:22 
GeneralHow to re-enable it? Pinmembermaxxon1517-Apr-11 5:46 
GeneralMy vote of 5 PinmemberJunfengGuo10-Apr-11 16:24 
GeneralMy vote of 1 PinmemberSharjith27-Mar-11 16:46 
GeneralRe: My vote of 1 PinmemberHowitZer2628-Mar-13 9:46 
GeneralRe: My vote of 1 PinmemberBehzad Sedighzadeh25-Mar-14 23:49 
GeneralBetter impossible - Thanks Pinmemberjoao pedro10-Mar-11 9:55 
GeneralMy vote of 5 PinmemberPriyaVaalu7-Feb-11 18:05 
GeneralMy vote of 5 Pinmemberaditya1989728-Jan-11 6:42 
GeneralThank U PinmemberKavyashri30-Mar-10 1:44 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 13 Sep 2007
Article Copyright 2007 by Giri Ganji
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid