C# desktop app disable the close button
I am writing a C# 2010 desktop application. I would like to disable the close button on the windows form. I only want the user to exit the application by clicking the exit button on the application. Thus can you tell me how to disable the close button on the windows form application?
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

Sanjeeb LenkaRank #301 · 8.3K reputationPosted Jun 27, 2013, 11:20 AM EDT
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;
}
}
or
you can follow this link
http://www.codeproject.com/Articles/20379/Disabling-Close-Button-on-Forms
VulpesRank #55 · 29.2K reputationPosted Jun 27, 2013, 11:24 AM EDT
1. Set the form's ControlBox property to false. However, this not only removes the X button, it all removes the Maximize and Minimize buttons as well.
2. You can capture the pressing of the X button in the Form's FormClosing event, tell the user that it can only be closed using the Exit button and then cancel the closure operation.
3. You can disable the X button (gray it out) by inserting the code which Sanjeeb posted in your Form class.