Click here to Skip to main content
Licence CPOL
First Posted 7 Sep 2007
Views 77,608
Bookmarked 75 times

Disabling Close Button on Forms

By | 12 Sep 2007 | Article
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)

About the Author

Giri Ganji

Web Developer

India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 4 PinmemberItz.Irshad18:35 3 Apr '12  
GeneralMy vote of 5 Pinmemberkharlos6:35 5 Sep '11  
Questionnot bad PinmemberCIDev7:22 15 Jul '11  
GeneralHow to re-enable it? Pinmembermaxxon155:46 17 Apr '11  
GeneralMy vote of 5 PinmemberJunfengGuo16:24 10 Apr '11  
GeneralMy vote of 1 PinmemberSharjith16:46 27 Mar '11  
GeneralBetter impossible - Thanks Pinmemberjoao pedro9:55 10 Mar '11  
GeneralMy vote of 5 PinmemberPriyaVaalu18:05 7 Feb '11  
GeneralMy vote of 5 Pinmemberaditya198976:42 28 Jan '11  
GeneralThank U PinmemberKavyashri1:44 30 Mar '10  
Generalthanks it works Pinmemberarchana_Kalyankar0:05 8 Dec '09  
GeneralExcelent ! PinmemberDrak_Mrak21:44 28 Sep '09  
QuestionHow to get confirmation from the user ? when he clicks Close Button? PinmemberNagendran1:20 20 Dec '08  
AnswerRe: How to get confirmation from the user ? when he clicks Close Button? PinmemberGiri Ganji18:17 21 Dec '08  
QuestionTooltip for 'close' button Pinmembermahone0:14 12 Dec '08  
GeneralGreat!!!!!!!!!!!!!!!!!!!! Pinmembergtag20:18 11 Nov '08  
GeneralThank you PinmemberCoreMax8:12 12 Oct '08  
GeneralWrong solution! PinmemberArjang Assadi18:35 19 Sep '07  
GeneralNOT a Wrong solution ! PinmemberThesteph5:43 17 Jul '08  
GeneralRe: NOT a Wrong solution ! PinmemberArjang Assadi13:31 17 Jul '08  
GeneralRe: NOT a Wrong solution ! PinmemberArjang Assadi5:36 18 Jul '08  
GeneralMethod to re-enable PinmemberGabriel Graves15:57 12 Sep '07  
GeneralRe: Method to re-enable Pinmemberwang_lihua5:31 14 Sep '07  
GeneralRe: Method to re-enable PinmemberGabriel Graves6:16 14 Sep '07  
GeneralRe: Method to re-enable PinmemberMember 13856983:06 11 Aug '08  

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.

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