Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / C#
Article

Disabling Close Button on Forms

Rate me:
Please Sign up or sign in to vote.
4.80/5 (69 votes)
12 Sep 2007CPOL2 min read 278.7K   240   88   55
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.

C#
//
// 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)


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

Comments and Discussions

 
QuestionThe Minimize functionality is not working correctly on first minimize Pin
ChrisRaisin20-Jun-22 4:02
ChrisRaisin20-Jun-22 4:02 
QuestionError Pin
Member 1263742626-Nov-16 5:41
Member 1263742626-Nov-16 5:41 
QuestionThanks a lot Pin
Member 267444231-Jul-16 18:22
Member 267444231-Jul-16 18:22 
QuestionGood Work Pin
VDKK24-Mar-16 1:18
VDKK24-Mar-16 1:18 
QuestionBut I can go to the taskbar and choose "Close Window" Pin
Nighty666624-Sep-15 4:56
Nighty666624-Sep-15 4:56 
QuestionThank you! Pin
K K Allentown1-Oct-14 5:42
K K Allentown1-Oct-14 5:42 
GeneralExcellent Pin
Francesco Giossi17-Jun-14 23:24
Francesco Giossi17-Jun-14 23:24 
GeneralRe: Excellent Pin
spencepk30-Jul-14 4:19
spencepk30-Jul-14 4:19 
QuestionNice piece of code Pin
sysmaniax12-Nov-13 23:00
sysmaniax12-Nov-13 23:00 
GeneralWorks great ! Pin
TransformYa19-Aug-13 0:08
professionalTransformYa19-Aug-13 0:08 
GeneralMy vote of 5 Pin
Member 977323922-Apr-13 23:08
Member 977323922-Apr-13 23:08 
QuestionI vote 5 Pin
Moxxis29-Mar-13 10:41
Moxxis29-Mar-13 10:41 
GeneralMy vote of 5 Pin
HowitZer2628-Mar-13 9:43
HowitZer2628-Mar-13 9:43 
QuestionOknevermind Pin
Branimir4427-Mar-13 21:08
Branimir4427-Mar-13 21:08 
GeneralMy vote of 5 Pin
Mahyar.FF22-Dec-12 2:02
Mahyar.FF22-Dec-12 2:02 
GeneralMy vote of 5 Pin
supernatik12-Dec-12 22:09
supernatik12-Dec-12 22:09 
GeneralMy vote of 5 Pin
Andrewpeter9-Sep-12 22:40
Andrewpeter9-Sep-12 22:40 
Questionakhilesh Pin
Member 928654531-Aug-12 19:23
Member 928654531-Aug-12 19:23 
GeneralMy vote of 4 Pin
Itz.Irshad3-Apr-12 18:35
Itz.Irshad3-Apr-12 18:35 
GeneralMy vote of 5 Pin
kharlos5-Sep-11 6:35
kharlos5-Sep-11 6:35 
Questionnot bad Pin
BillW3315-Jul-11 7:22
professionalBillW3315-Jul-11 7:22 
GeneralHow to re-enable it? Pin
maxxon1517-Apr-11 5:46
maxxon1517-Apr-11 5:46 
GeneralMy vote of 5 Pin
JunfengGuo10-Apr-11 16:24
JunfengGuo10-Apr-11 16:24 
GeneralMy vote of 1 Pin
Sharjith27-Mar-11 16:46
professionalSharjith27-Mar-11 16:46 
GeneralRe: My vote of 1 Pin
HowitZer2628-Mar-13 9:46
HowitZer2628-Mar-13 9:46 

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.