Thursday, March 01, 2001 ..:: Home ::.. Register  Login
   TechTidBits (Blog)  

How do you disable a WinForms close button?

Jun 5

Written by:
Friday, June 05, 2009 9:35 PM  RssIcon

How do you effectively disable the windows control box close button?  You know, the bit "X" on the top right hand side of every WinForm, how do you get rid of it/disable it?

Today I was setting up a testing program and for my own reasons, I wanted to control a windows control box buttons.  I didn't want to give the user the ability to use the big "X" button on the top control box.  But how do you do that?  I found out today there's a small trick, slight of hand if you will.  Here it is with a sample demo as well.

Notice on the top right, the close button, the "X" has been disabled.  Unfortunately that's the best I could do, I couldn't actually figure out how to completely make it disappear and still keep the form's icon visible. 

Disable the Close button 

Oh, and IF your thinking "oh, just set the ControlBox to false and think that'll work, nope, sorry.  It WILL remove the close button, yes, BUT it has the unfortunate side effect of ALSO removing the Windows icon which could be key to your company's/personal branding/marketing efforts.

Missing Window Icon

So, how did I do that slight of hand?  I can't take credit for it, I googled it and got lucky with my search string.  Thanks to Sushila Patel's blog: How To: Hide close button for Windows Form.

Here's the magical code. 

    protected override CreateParams CreateParams
        CreateParams param = base.CreateParams;
        param.ClassStyle = param.ClassStyle | 0x200;
        return param;

Time to go grab a coffee and get coding!



Source Code:

Sushila Patel's blog: How To: Hide close button for Windows Form


Location: Blogs Parent Separator TechTidBits

6 comment(s) so far...

Re: How do you disable a WinForms close button?

Hey Peter,

Here is another way using Interop by getting the Form's system menu and disabling the close menuitem which is basically the "x" on the right and the menuitem on the left if its present.

Miss the good old

Have fun.


using System.Runtime.InteropServices;

static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);

static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

internal const UInt32 SC_CLOSE = 0xF060;
internal const UInt32 MF_ENABLED = 0x00000000;
internal const UInt32 MF_GRAYED = 0x00000001;
internal const UInt32 MF_DISABLED = 0x00000002;
internal const uint MF_BYCOMMAND = 0x00000000;

public static void EnableDisableFormCloseButton( Form frm, bool bEnabled)
IntPtr hSystemMenu = GetSystemMenu(frm.Handle, false);
EnableMenuItem(hSystemMenu, SC_CLOSE, (uint)(MF_ENABLED | (bEnabled ? MF_ENABLED : MF_GRAYED)));


By Souheil Ibrahim on   Thursday, June 11, 2009 12:28 PM

Re: How do you disable a WinForms close button?

AH COOL! :> Awesome! Which way is the "suggested" way of doing it? Do they do the exact samething or is the Interop slightly different? Does it hide the button or disable it?

Thanks for reply by the way. :> Very cool.

By phenry on   Thursday, June 11, 2009 12:29 PM

Re: How do you disable a WinForms close button?

I havent seen a "suggested way" for doing that without interop.

And to answer your question, it does disable the "x" not hide. BUT, there is another catch which I am still looking into.

Here is the situation:

1) Create a from with minimize, maximize, close control box
2) Add a button and functionality to disable the "x" as mentioned above

Now run your form and click on the button, you will see that the "X" gets disabled...nice...well not that quite :(
if you minimize your form then restore it back, you will see theat "x" is enabled again...grrrrrr...
something is happening and the framework is reseting the states from internal memory...

One thing I am looking into are Private members "formState" and "formStateEx" they are bits and need to decode them and see how it works when I have a chance.


By Souheil Ibrahim on   Friday, June 12, 2009 1:33 PM

Re: How do you disable a WinForms close button?

Thank you for the heads up Souheil! :> Much appreciated! THanks for the GOTCHA as wel, I didn't know about that.

I looked at the code I had posted, and the minimize/restore gotcha you presented isn't apparent (PHEW!) with the CreateParams method. That seems like an OK solution. Thoughts?

By phenry on   Thursday, June 18, 2009 10:29 AM

Re: How do you disable a WinForms close button?

Am I right in thinking this solution will not work on a Mobile (pocket PC) form.

I have found several examples of this solution on various site, but when I insert the code into the form class code the "CreateParams" is unknown

any help most appreciated



By Chris Dent on   Thursday, August 20, 2009 12:37 PM

Re: How do you disable a WinForms close button?

Howdy Chris. I'm very sorry but I have next to no mobile/pocket pc dev to advice on this. Sorry about that, but if you do find out one way or the other, drop a comment here please.

By phenry on   Thursday, August 20, 2009 12:38 PM

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Add Comment   Cancel 
Copyright 1999-2012 by   Terms Of Use  Privacy Statement