There is no direct way to disbale the X button (in property ) in a Windows Form, like there is a property for Maximize button called MaximizeBox = false Or Minimize Box = false.
I have used Win32 API to do so. It is implemented by importing unmanaged DLL [user32] and calling it's functions.
Notes: Before you use code, Please add a Close button in your form so that you can close your app.
Add the following library
using System.Runtime.InteropServices;
Declare the following as class level variable
const int MF_BYPOSITION = 0x400;
[DllImport("User32")]
private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags);
[DllImport("User32")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("User32")]
private static extern int GetMenuItemCount(IntPtr hWnd);
In the Form_Load() event, write the following code
private void Form1_Load(object sender, EventArgs e)
{
IntPtr hMenu = GetSystemMenu(this.Handle, false);
int menuItemCount = GetMenuItemCount(hMenu);
RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION);
}
Hope it will help you all.
Cheers.


Albin LarssonPosted Sep 8, 2012, 11:21 AM EDT
Just was I was looking for!
Deepak BhatiaeditedPosted Aug 6, 2010, 2:20 AM EDTEdited Aug 6, 2010, 2:20 AM EDT
Really earlier I was looking for a certain application to get this functionality but was not able to find solutions. Great help.
Chinmay DurgeshPosted Feb 26, 2010, 3:35 AM EST
Hey, Thanks for the article. There is another very simpler way. All you need to do is, set the "ControlBox" property to false. Hope this helps.
chattolearnPosted Jan 23, 2010, 2:37 AM EST
hi. check here for another solution which I think easier. the solution you mentioned is also nice. http://www.php24hours.com/c-sharp-how-to-disable-the-close-button-t164.html good luck
swarnalaxmi sbeditedPosted Jan 3, 2010, 9:18 PM ESTEdited Jan 3, 2010, 9:20 PM EST
Thank you very much. This article is helpful to me