Click here to Skip to main content
15,885,767 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 279.2K   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

 
GeneralNOT a Wrong solution ! Pin
Thesteph17-Jul-08 5:43
Thesteph17-Jul-08 5:43 
GeneralRe: NOT a Wrong solution ! Pin
UniversalConstructs17-Jul-08 13:31
UniversalConstructs17-Jul-08 13:31 
GeneralRe: Wrong solution! Pin
HowitZer2628-Mar-13 9:59
HowitZer2628-Mar-13 9:59 
GeneralMethod to re-enable Pin
Gabriel Graves12-Sep-07 15:57
Gabriel Graves12-Sep-07 15:57 
GeneralRe: Method to re-enable Pin
wang_lihua14-Sep-07 5:31
wang_lihua14-Sep-07 5:31 
GeneralRe: Method to re-enable Pin
Gabriel Graves14-Sep-07 6:16
Gabriel Graves14-Sep-07 6:16 
GeneralRe: Method to re-enable Pin
Zeek211-Aug-08 3:06
Zeek211-Aug-08 3:06 
GeneralAlternative/Better Way Pin
Hugo Caldeira10-Sep-07 13:53
Hugo Caldeira10-Sep-07 13:53 
This way you can control when the close button of the form is disabled/enabled.

the "chkCanClose" variable represents a CheckBox

[System.Runtime.InteropServices.DllImport("User32")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

[System.Runtime.InteropServices.DllImport("User32")]
private static extern bool EnableMenuItem(IntPtr hMenu, int uIDEnableItem, int uEnable);

private const int SC_CLOSE = 0x0F060;
private const int MF_BYCOMMAND = 0x0;
private const int MF_ENABLED = 0x0;
private const int MF_GRAYED = 0x1;
private const int MF_DISABLED = 0x2;

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
UpdateClose();
}

// Here we must update button because after maximize/restore
// somehow the close button gets enabled again, this is an
// effective way to re-disable the button
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
UpdateClose();
}

protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
e.Cancel = !chkCanClose.Checked;
}

private void UpdateClose()
{
IntPtr hSysMenu = GetSystemMenu(this.Handle, false);
int flags = MF_BYCOMMAND;
if (chkCanClose.Checked)
flags |= MF_ENABLED;
else
flags |= MF_DISABLED | MF_GRAYED;
EnableMenuItem(hSysMenu, SC_CLOSE, flags);
}

// Event handler for checkbox
private void chkCanClose_CheckedChanged(object sender, EventArgs e)
{
UpdateClose();
}

GeneralRe: Alternative/Better Way Pin
Member 372519615-Sep-08 20:20
Member 372519615-Sep-08 20:20 
GeneralGood Pin
Pooya Musavi7-Sep-07 9:36
Pooya Musavi7-Sep-07 9:36 
GeneralNow what Pin
Steve Messer7-Sep-07 4:31
Steve Messer7-Sep-07 4:31 
GeneralRe: Now what Pin
Giri Ganji27-Aug-08 19:10
Giri Ganji27-Aug-08 19:10 
GeneralRe: Now what Pin
HowitZer2628-Mar-13 9:48
HowitZer2628-Mar-13 9:48 
Generalsimple.... Pin
bkaratte7-Sep-07 3:35
bkaratte7-Sep-07 3:35 
GeneralRe: simple.... Pin
HowitZer2628-Mar-13 9:49
HowitZer2628-Mar-13 9:49 
GeneralExcellent Pin
Sitten Spynne7-Sep-07 3:12
Sitten Spynne7-Sep-07 3:12 
GeneralGreat! Pin
Sorantis7-Sep-07 2:54
Sorantis7-Sep-07 2:54 

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.