One way is to set the ControlBox property of the form to false in the form's properties panel, or write it like this in the form's constructor:
Copy the codeThe code is as follows:
public Form1()
{
InitializeComponent();
= false; // Set the close button not to appear
}
However, if you do this, you will get rid of the minimize and maximize buttons. So, if you just want the close button to not work and then keep the minimize and maximize, rewrite the CreateParams method of the form:
Copy the codeThe code is as follows:
//Disable the form's close button
private const int CP_NOCLOSE_BUTTON = 0x200;
protected override CreateParams CreateParams
{
get
{
CreateParams myCp = ;
= | CP_NOCLOSE_BUTTON;
return myCp;
}
}
Or cancel the closing event execution in the upper left corner
Copy the codeThe code is as follows:
private void Form1_Closing(object sender, e)
{
= true;
}
// Rewrite OnClosing to enable the form to be indented into the tray when clicking the close button
protected override void OnClosing(CancelEventArgs e)
{
= false;
= ;
= true;
}