SIGN UP MEMBER LOGIN:    
ARTICLE

How do I disable the Close 'X' button in a Windows Form using C#

Posted by Rajendra Tripathy Articles | How do I August 29, 2008
There are methods to disable Maximize and Minimize buttons of a Windows Form. This tip shows how to disable the Close button in of a form.
Reader Level:

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.

NotesBefore 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.

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks buddy..

Posted by Rajendra Tripathy Jun 08, 2011

Really earlier I was looking for a certain application to get this functionality but was not able to find solutions.
Great help.

Posted by Deepak Bhatia Aug 06, 2010

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.

Posted by Chinmay Durgesh Feb 26, 2010

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

Posted by chattolearn Jan 23, 2010

Thank you very much.
This article is helpful to me

Posted by swarnalaxmi sb Jan 04, 2010
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor