How to Disable Close Button in window form?


Disabling Close Button in window form is much pretty easy.
First import the below namespace to resolve DllImport class.

using System.Runtime.InteropServices;


Write the below line of code on Form Load event.

//Disable Close Button-----------Start Here
IntPtr hWnd = SystemMenuList(this.Handle, false);
int nPosition = MenuItemCount(hWnd);
const int wFlag = 0x400;
RemoveMenu(hMenu, nPosition - 1, wFlag);
//Disable Close Button-----------End Here


Add below methods to your form.

#region Disable Close Button

[DllImport("User32")]
private static extern IntPtr SystemMenuList(IntPtr hWnd, bool bRevert);

[DllImport("User32")]
private static extern int MenuItemCount(IntPtr hWnd);

[DllImport("User32")]
private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlag);

#endregion