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;


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


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


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.


Comments (35)
  1. Anonymous says:

    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.

  2. Anonymous says:

    thanks alot !! .. this article is very useful..

    thank u.

  3. Anonymous says:

    where is closebox johnV its not in .net

  4. Anonymous says:

    I was like you and googled a bit for a solution and found nothing but VB examples. and I came across your article. Your code works nice thanks for converting.  But like you i wanted a managed solution. I came across this and thought i would share.. Here is another article which removes the "x" completely using ( this.ControlBox = false; )


  5. Anonymous says:

    Very useful article.


  6. Anonymous says:

    It was a very useful article.  Thanks a lot.

  7. Anonymous says:

    Gt properties setting in VS2005, just set to false on "Control Box"


  8. Rani Sarangi says:

    To all those who gave the solution Set Control Box property to False…

    If you do this, then it will take away the min, max and close buttons also. What if you just want to disable the close button only but keep the min and max buttons?

  9. Anonymous says:

    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.

  10. Anonymous says:

    Thank you very much,

    that was a simple yet elegant solution , bulls eye.

    Sreenivas Kapila

  11. Anonymous says:

    Example taken from:



    // source code

    // Code Snippet

    private const int CP_NOCLOSE_BUTTON = 0x200;

    protected override CreateParams CreateParams




           CreateParams myCp = base.CreateParams;

           myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON ;

           return myCp;



  12. Rani Sarangi says:

    bwing, the codeproject blog you have mentioned was published months after this blog, perhaps you can do the math now :)…

  13. Anonymous says:


    nice and easy!

    thanks a lot!

  14. Anonymous says:

    Nice! It allows you to simulate the behaviour of a MessageBox dialog. Very usefull, thanks.

  15. Anonymous says:

    there is a way for ADD a menù botton??

  16. Anonymous says:


    Very nice article ..

    But do some one know how to remove the maximize button?? 🙂


  17. Anonymous says:

    I Found it, Thanks, 😉


  18. Anonymous says:

    Thank you so much…

    This is real usefull and intresting.

  19. Anonymous says:

    Thanks very very very good you code, thx.

  20. Anonymous says:

    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.


  21. Anonymous says:


    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.


  22. Anonymous says:

    Sweet, works great, and very easy to implement 🙂

  23. Anonymous says:


    thank you for your solution with CreateParam!!!!



  24. Anonymous says:

    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??

  25. Anonymous says:

    Very useful.  Thanks for writing this up!

  26. Anonymous says:

    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..

  27. Anonymous says:

    Look in the properties panel, see controlbox, change it to false.

  28. Anonymous says:

    All three options , minimize, close and maximize can be done without code through the properties panel

  29. Amolpbhavsar says:

    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.

  30. Anonymous says:

    waw, really deep in API system.. dont understand anything ^^

  31. Anonymous says:

    It works and saved me a lot of research time, but I hope it doesnot have any overhead associated with it

  32. Anonymous says:

    In .Net , it would be the control box, instead of close box. This will get rid of everything but the title.

  33. Anonymous says:

    Thank you ,great Solution , I used GreatParm and it worked. any Solution to remove ( not disable ) Max button ?

  34. Anonymous says:

    Thank Bro (Y) i was searching for a long time.

Comments are closed.

Skip to main content