Disable Close 'X' button in Winforms using C#
By rajendra tripathy May 29, 2008
By rajendra tripathy May 29, 2008
There are methods to disable Maximize and Minimize button of windows form. Here in this article, we will see how to disable close button in windows form.
There is no direct way to disbale the X button (in property ), like there is a property for Maximize button called MaximizeBox = false Or Minimize Box = false.
This 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; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Windows.Forms; namespace FileCollect { class TestForm:Form { 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); private void Form1_Load(object sender, EventArgs e) { IntPtr hMenu = GetSystemMenu(this.Handle, false); int menuItemCount = GetMenuItemCount(hMenu); RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION); } } }