Visual C#: Handling X button (top-right)
Posted: 7 Dec 2009, 10:21am - Monday

xbuttonDisabling the X button

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;
     }
}
Add confirmation if window closing or closed... Insert this code to Main form or the Form1.cs...
private void Form1_Closing(object sender,
                   System.ComponentModel.CancelEventArgs e)
{
     if (MessageBox.Show("Are you sure you want to exit?", "Confirm exit",
         MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
     {
        e.Cancel = true;
     }
}
Insert this to Form1.Designer.cs...
this.Closing += new System.ComponentModel.CancelEventHandler(
                             this.frmCCS_Closing);