Partilhar via


Disable Close X button in Winforms using C#

Demo code on how to disable the X button in menu bar in a window. I found a lot of posts on this in VB, but none for C#. So if you are a C# fan like me, this is for you...

1. There is no direct way to disbale the X button, like there is a property for Maximize button called MaximizeBox = false.

2. This is implemented by importing unmanaged dll "user32" and calling it's functions.

3. Before you use this code, make sure to add a Close button in your form so that you can close your app. See the attached screen shot Disabled.jpg. Below is the code for this form.

Add the following library

using

System.Runtime.InteropServices;

Declare the following as class level variable

const

int MF_BYPOSITION = 0x400;

[

DllImport("User32")]

private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags);

[

DllImport("User32")]

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

[

DllImport("User32")]

private static extern int GetMenuItemCount(IntPtr hWnd);

In the Form_Load() event, write the following code:

private void Form1_Load(object sender, EventArgs e)

{

IntPtr hMenu = GetSystemMenu(this.Handle, false);

int menuItemCount = GetMenuItemCount(hMenu);

RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION);

}

4. Run it. Voila! you are done. If you know of a ay to do it without calling unmanaged code, please do let me know.

Disabled.JPG

Comments

  • Anonymous
    May 19, 2007
    If you set closebox to false it will get rid of everything but the title. And then settting the title to "" (empty string) you can get rid of the title also.
  • Anonymous
    June 02, 2007
    thanks alot !! .. this article is very useful..thank u.
  • Anonymous
    June 08, 2007
    where is closebox johnV its not in .net
  • Anonymous
    June 14, 2007
    The comment has been removed
  • Anonymous
    June 29, 2007
    Very useful article.Thanks.
  • Anonymous
    July 19, 2007
    It was a very useful article.  Thanks a lot.
  • Anonymous
    September 17, 2007
    The comment has been removed
  • Anonymous
    October 16, 2007
    The comment has been removed
  • Anonymous
    December 21, 2007
    I also had to add an event handler for FormClosing event, or else pressing Ctrl-F4 in my modeless window would close it just as if clicking on the Close button.
  • Anonymous
    April 19, 2008
    Thank you very much,that was a simple yet elegant solution , bulls eye.Sreenivas Kapila
  • Anonymous
    April 22, 2008
    Example taken from:http://www.codeproject.com/KB/cs/DisableClose.aspx//// source code// Code Snippetprivate const int CP_NOCLOSE_BUTTON = 0x200;protected override CreateParams CreateParams{    get    {       CreateParams myCp = base.CreateParams;       myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON ;       return myCp;    }}
  • Anonymous
    May 03, 2008
    bwing, the codeproject blog you have mentioned was published months after this blog, perhaps you can do the math now :)...
  • Anonymous
    July 28, 2008
    Great!!nice and easy!thanks a lot!
  • Anonymous
    August 14, 2008
    Nice! It allows you to simulate the behaviour of a MessageBox dialog. Very usefull, thanks.
  • Anonymous
    January 11, 2009
    there is a way for ADD a menù botton??
  • Anonymous
    January 13, 2009
    Hi..Very nice article ..But do some one know how to remove the maximize button?? :)Hamudi,..
  • Anonymous
    January 13, 2009
    I Found it, Thanks, ;)Hamudi
  • Anonymous
    February 09, 2009
    Thank you so much...This is real usefull and intresting.
  • Anonymous
    March 20, 2009
    Thanks very very very good you code, thx.
  • Anonymous
    May 21, 2009
    It's nice article, but when I run my application, the close button is disabled but still I can press Ctrl+F4 and it works. It closes my form.Thanks!
  • Anonymous
    June 08, 2009
    Umesh,You must have a CLOSE button on your form. On click event of this button set a global variable like (bool CANCLOSE = true;). The default value of CANCLOSE should be false.Now, in your Form1_FormClosing() event add this:e.Cancel = CANCLOSE==false;You are all set now.Mehdi
  • Anonymous
    June 14, 2009
    Sweet, works great, and very easy to implement :)
  • Anonymous
    January 22, 2010
    Look here for an easy solution.http://www.php24hours.com/c-sharp-how-to-disable-the-close-button-t164.htmlbiz
  • Anonymous
    April 20, 2010
    bwing,thank you for your solution with CreateParam!!!!Loveit--danky
  • Anonymous
    November 04, 2010
    Why did microsoft program it in such a way compared to previous form designs, that you need a huge amount of code to do what you want 'closebox  = false?  this is meant to be object orientated, what's their logic??
  • Anonymous
    November 11, 2010
    Very useful.  Thanks for writing this up!
  • Anonymous
    December 23, 2010
    Thanks dear i got whatever i want. but may i know is there any way we can hide the 'X' close button in the form?thank you once again..
  • Anonymous
    February 21, 2011
    Look in the properties panel, see controlbox, change it to false.
  • Anonymous
    February 21, 2011
    All three options , minimize, close and maximize can be done without code through the properties panel
  • Anonymous
    February 23, 2011
    Is there any way to enable or disable the CLOSE button as per the requirement. The current implementation tells us only to Disable the CLOSE button.
  • Anonymous
    April 06, 2011
    waw, really deep in API system.. dont understand anything ^^
  • Anonymous
    October 05, 2011
    It works and saved me a lot of research time, but I hope it doesnot have any overhead associated with it
  • Anonymous
    December 30, 2011
    The comment has been removed
  • Anonymous
    February 13, 2012
    Thank you ,great Solution , I used GreatParm and it worked. any Solution to remove ( not disable ) Max button ?
  • Anonymous
    November 27, 2013
    Thank Bro (Y) i was searching for a long time.