Click here to Skip to main content
11,716,249 members (84,203 online)
Click here to Skip to main content

Disabling Close Button on Forms

, 12 Sep 2007 CPOL 149.4K 240 88
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

You may also be interested in...

Comments and Discussions

 
QuestionThank you! Pin
K K Allentown1-Oct-14 5:42
memberK K Allentown1-Oct-14 5:42 
GeneralExcellent Pin
Francesco Giossi17-Jun-14 23:24
memberFrancesco Giossi17-Jun-14 23:24 
GeneralRe: Excellent Pin
spencepk30-Jul-14 4:19
memberspencepk30-Jul-14 4:19 
QuestionNice piece of code Pin
sysmaniax12-Nov-13 23:00
membersysmaniax12-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
memberMember 977323922-Apr-13 23:08 
QuestionI vote 5 Pin
Moxxis29-Mar-13 10:41
memberMoxxis29-Mar-13 10:41 
Nicely done.
GeneralMy vote of 5 Pin
HowitZer2628-Mar-13 9:43
memberHowitZer2628-Mar-13 9:43 
QuestionOknevermind Pin
Branimir4427-Mar-13 21:08
memberBranimir4427-Mar-13 21:08 
GeneralMy vote of 5 Pin
Mahyar.FF22-Dec-12 2:02
memberMahyar.FF22-Dec-12 2:02 
GeneralMy vote of 5 Pin
supernatik12-Dec-12 22:09
membersupernatik12-Dec-12 22:09 
GeneralMy vote of 5 Pin
Andrewpeter9-Sep-12 22:40
memberAndrewpeter9-Sep-12 22:40 
Questionakhilesh Pin
Member 928654531-Aug-12 19:23
memberMember 928654531-Aug-12 19:23 
GeneralMy vote of 4 Pin
Itz.Irshad3-Apr-12 18:35
memberItz.Irshad3-Apr-12 18:35 
GeneralMy vote of 5 Pin
kharlos5-Sep-11 6:35
memberkharlos5-Sep-11 6:35 
Questionnot bad Pin
CIDev15-Jul-11 7:22
memberCIDev15-Jul-11 7:22 
GeneralHow to re-enable it? Pin
maxxon1517-Apr-11 5:46
membermaxxon1517-Apr-11 5:46 
GeneralMy vote of 5 Pin
JunfengGuo10-Apr-11 16:24
memberJunfengGuo10-Apr-11 16:24 
GeneralMy vote of 1 Pin
Sharjith27-Mar-11 16:46
memberSharjith27-Mar-11 16:46 
GeneralRe: My vote of 1 Pin
HowitZer2628-Mar-13 9:46
memberHowitZer2628-Mar-13 9:46 
GeneralRe: My vote of 1 Pin
Behzad Sedighzadeh25-Mar-14 23:49
memberBehzad Sedighzadeh25-Mar-14 23:49 
GeneralBetter impossible - Thanks Pin
joao pedro10-Mar-11 9:55
memberjoao pedro10-Mar-11 9:55 
GeneralMy vote of 5 Pin
PriyaVaalu7-Feb-11 18:05
memberPriyaVaalu7-Feb-11 18:05 
GeneralMy vote of 5 Pin
aditya1989728-Jan-11 6:42
memberaditya1989728-Jan-11 6:42 
GeneralThank U Pin
Kavyashri30-Mar-10 1:44
memberKavyashri30-Mar-10 1:44 
Generalthanks it works Pin
archana_Kalyankar8-Dec-09 0:05
memberarchana_Kalyankar8-Dec-09 0:05 
GeneralExcelent ! Pin
Drak_Mrak28-Sep-09 21:44
memberDrak_Mrak28-Sep-09 21:44 
QuestionHow to get confirmation from the user ? when he clicks Close Button? Pin
Nagendran20-Dec-08 1:20
memberNagendran20-Dec-08 1:20 
AnswerRe: How to get confirmation from the user ? when he clicks Close Button? Pin
Giri Ganji21-Dec-08 18:17
memberGiri Ganji21-Dec-08 18:17 
QuestionTooltip for 'close' button Pin
mahone12-Dec-08 0:14
membermahone12-Dec-08 0:14 
GeneralGreat!!!!!!!!!!!!!!!!!!!! Pin
gtag11-Nov-08 20:18
membergtag11-Nov-08 20:18 
GeneralThank you Pin
CoreMax12-Oct-08 8:12
memberCoreMax12-Oct-08 8:12 
GeneralWrong solution! Pin
Arjang Assadi19-Sep-07 18:35
memberArjang Assadi19-Sep-07 18:35 
GeneralNOT a Wrong solution ! Pin
Thesteph17-Jul-08 5:43
memberThesteph17-Jul-08 5:43 
GeneralRe: NOT a Wrong solution ! Pin
Arjang Assadi17-Jul-08 13:31
memberArjang Assadi17-Jul-08 13:31 
GeneralRe: Wrong solution! Pin
HowitZer2628-Mar-13 9:59
memberHowitZer2628-Mar-13 9:59 
GeneralMethod to re-enable Pin
Gabriel Graves12-Sep-07 15:57
memberGabriel Graves12-Sep-07 15:57 
GeneralRe: Method to re-enable Pin
wang_lihua14-Sep-07 5:31
memberwang_lihua14-Sep-07 5:31 
GeneralRe: Method to re-enable Pin
Gabriel Graves14-Sep-07 6:16
memberGabriel Graves14-Sep-07 6:16 
GeneralRe: Method to re-enable Pin
Member 138569811-Aug-08 3:06
memberMember 138569811-Aug-08 3:06 
GeneralAlternative/Better Way Pin
Hugo Caldeira10-Sep-07 13:53
memberHugo Caldeira10-Sep-07 13:53 
GeneralRe: Alternative/Better Way Pin
Member 372519615-Sep-08 20:20
memberMember 372519615-Sep-08 20:20 
GeneralGood Pin
Pooya Musavi7-Sep-07 9:36
memberPooya Musavi7-Sep-07 9:36 
GeneralNow what Pin
smesser7-Sep-07 4:31
membersmesser7-Sep-07 4:31 
GeneralRe: Now what Pin
Giri Ganji27-Aug-08 19:10
memberGiri Ganji27-Aug-08 19:10 
GeneralRe: Now what Pin
HowitZer2628-Mar-13 9:48
memberHowitZer2628-Mar-13 9:48 
Generalsimple.... Pin
bkaratte7-Sep-07 3:35
memberbkaratte7-Sep-07 3:35 
GeneralRe: simple.... Pin
HowitZer2628-Mar-13 9:49
memberHowitZer2628-Mar-13 9:49 
GeneralExcellent Pin
Sitten Spynne7-Sep-07 3:12
memberSitten Spynne7-Sep-07 3:12 
GeneralGreat! Pin
Sorantis7-Sep-07 2:54
memberSorantis7-Sep-07 2:54 

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 | Terms of Use | Mobile
Web04 | 2.8.150901.1 | Last Updated 13 Sep 2007
Article Copyright 2007 by Giri Ganji
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid