How to Disable Close Button in Title bar of a Windows Form

No.of Views3045
Bookmarked0 times
Downloads 
Votes0
By  abhi2434   On  11 Nov 2010 09:11:14
Tag : CSharp , Windows Forms
In this snippet, i will show to How to Disable Close Button in Title bar of a Windows Form
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this snippet, i will show to How to Disable Close Button in Title bar of a Windows Form.As a developer, you must have found few things that are really haven't thought of earlier. On that note, Say you want to disable Close Button of a window. It is easier to disable other button (using properties available with Form) from control box of the window, or even remove the control box altogether.

Implementation

But If you still like to disable the Close button Just follow the steps :

1. Add this using Statement in the header

using System.Runtime.InteropServices;

2. In the class add .

private 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);

 3. Finally to disable the close button during runtime Use ,

IntPtr hMenu = GetSystemMenu(this.Handle, false);
int menuItemCount = GetMenuItemCount(hMenu);
RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION);

 You are done. You will actually see a window with disabled close button. Thank you reading.

 
Sign Up to vote for this article
 
About Author
 
abhi2434
Occupation-Not Provided
Company-Not Provided
Member Type-Senior
Location-Not Provided
Joined date-22 Oct 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top