How to disable the X button on a Windows Form?

How can you disable the “X” button on a Windows Form so that user has to click a Close button on the form to close the window?

You may say just set ControlBox=False for the Form. But it will hide all the buttons, including the minimize and the maximize button. What if you still want user to be able to minimize/maximize the window?

Jose Luis Manners described a very simple but slick way to accomplish this task by overriding CreateParams method.

Here is the code:

protected override CreateParams CreateParams

{

get

{

const int CS_NOCLOSE = 0x200;

CreateParams cp = new CreateParams();

cp = base.CreateParams;

cp.ClassStyle = cp.ClassStyle | CS_NOCLOSE;

return cp;

}

}

Leave a Reply

avatar
  Subscribe  
Notify of