23 Jun

C# Disable Or Hide Close Button(X) in Windows Form

This examples show how to hide/disable close button(X) in C#. To achieve this, we must override the CreateParams method in Form class.

Disable Close Button:

            
        //Disable close button
        private const int CP_DISABLE_CLOSE_BUTTON = 0x200;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ClassStyle = cp.ClassStyle | CP_DISABLE_CLOSE_BUTTON;
                return cp;
            }
        }

Disable Close Button

Remove The Entire System Menu:

        //remove the entire system menu:
        private const int WS_SYSMENU = 0x80000;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.Style &= ~WS_SYSMENU;
                return cp;
            }
        }            

Remove Entire System Menu

One thought on “C# Disable Or Hide Close Button(X) in Windows Form

Leave a Reply

Your email address will not be published. Required fields are marked *