How to

disable close 'x' button in winform

Published: 15. December 2008 | Updated: 15. December 2008
License: Microsoft Public License (MS-PL)
Categories: Windows » Win32, Winforms
Tags: C# UI Windows Winforms
Was this snippet helpful for you? YESYES / NONO
Get Analyzant!

Check your entire website from code to content in minutes with Analyzant, the free analysis suite.

Free download at

Snippet shows how to disable close 'x' button in Winforms

Import namespace

using System.Runtime.InteropServices;

Import extern methods

const int MF_BYPOSITION = 0x400;

private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags);

private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

private static extern int GetMenuItemCount(IntPtr hWnd);


IntPtr hMenu = GetSystemMenu(this.Handle, false);
int menuItemCount = GetMenuItemCount(hMenu);
RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION);
Send us feedback about this snippet »

Related Snippets: