Click here to Skip to main content
Click here to Skip to main content

Disabling Close Button on Forms

By , 12 Sep 2007
 
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
No Biography provided

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5 PinmemberMember 977323922 Apr '13 - 23:08 
QuestionI vote 5 PinmemberMoxxis29 Mar '13 - 10:41 
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 
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 
Generalthanks it works Pinmemberarchana_Kalyankar8 Dec '09 - 0:05 
GeneralExcelent ! PinmemberDrak_Mrak28 Sep '09 - 21:44 
QuestionHow to get confirmation from the user ? when he clicks Close Button? PinmemberNagendran20 Dec '08 - 1:20 
QuestionTooltip for 'close' button Pinmembermahone12 Dec '08 - 0:14 
GeneralGreat!!!!!!!!!!!!!!!!!!!! Pinmembergtag11 Nov '08 - 20:18 
GeneralThank you PinmemberCoreMax12 Oct '08 - 8:12 
GeneralWrong solution! PinmemberArjang Assadi19 Sep '07 - 18:35 
GeneralMethod to re-enable PinmemberGabriel Graves12 Sep '07 - 15:57 
GeneralAlternative/Better Way PinmemberHugo Caldeira10 Sep '07 - 13:53 
GeneralGood PinmemberPooya Musavi7 Sep '07 - 9:36 
GeneralNow what Pinmembersmesser7 Sep '07 - 4:31 
Generalsimple.... Pinmemberbkaratte7 Sep '07 - 3:35 
GeneralExcellent PinmemberSitten Spynne7 Sep '07 - 3:12 
GeneralGreat! PinmemberSorantis7 Sep '07 - 2:54 

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

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