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 (35)

  1. JohnV 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. kyi thar says:

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

    thank u.

  3. bob says:

    where is closebox johnV its not in .net

  4. mitch 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; )

    http://einfall.blogspot.com/2006/09/removing-close-box-from-c-form.html

  5. JG Vimalan says:

    Very useful article.

    Thanks.

  6. Karthik Murali says:

    It was a very useful article.  Thanks a lot.

  7. Wallace 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. Jimmy 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. Sreenivas Kapila says:

    Thank you very much,

    that was a simple yet elegant solution , bulls eye.

    Sreenivas Kapila

  11. bwing says:

    Example taken from:

    http://www.codeproject.com/KB/cs/DisableClose.aspx

    //

    // 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;

        }

    }

  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. Mie-G says:

    Great!!

    nice and easy!

    thanks a lot!

  14. edgar says:

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

  15. benetz says:

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

  16. Hamudi says:

    Hi..

    Very nice article ..

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

    Hamudi,..

  17. Hamudi says:

    I Found it, Thanks, 😉

    Hamudi

  18. MRKG! says:

    Thank you so much…

    This is real usefull and intresting.

  19. Thiago Moreira says:

    Thanks very very very good you code, thx.

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

    Thanks!

  21. Mehdi Maskoot says:

    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

  22. Sweet, works great, and very easy to implement 🙂

  23. danky says:

    bwing,

    thank you for your solution with CreateParam!!!!

    Loveit

    –danky

  24. Why 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. Lander says:

    Very useful.  Thanks for writing this up!

  26. Hashim 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. Visual Studio 2010 - Remove close option says:

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

  28. Visual Studio - C# 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. choco says:

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

  31. Adewole Ayobami says:

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

  32. Alec says:

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

  33. Mehran says:

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

  34. Fero says:

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

Skip to main content