Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

FREE 12 month online training for ASP.NET & MS Expression Studio and a Free copy of MS Expression Web with Windows Server Purchase
How To Disable Close Button in Windows Form

Author: DevASP
Download Source Code : 307_CloseButton.zip

In this article i will try to explain you how you can disable Close Button in you windows application using Visual Basic.Net (VB.NET)

In some situation during your project you did not need or want the close button of your form. This article is about how you can disable the close button of your form using the window API’s.

 

  • Steps you will do
  • Start visual studio and create a new window application.
  • Drop two button controls on the form.
  • Set the properties of the First as below:

Name = btnDisable, Text = Disable Close Button

 

  • Set the properties of the Second as below:

Name = btnClose, Text = Close Me

 

  • Drop a label control on your form and set its Text property as below:

Text = It is very simple to disable the close button of form.

 

  • Declare the following functions:

Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As IntPtr, ByVal nPosition As Integer, ByVal wFlags As Long) As IntPtr

 

Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As IntPtr, ByVal bRevert As Boolean) As IntPtr

 

Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As IntPtr) As Integer

 

Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As IntPtr) As Boolean

 

Private Const MF_BYPOSITION = &H400

Private Const MF_REMOVE = &H1000

Private Const MF_DISABLED = &H2

 

  • Now write the following function which will disable the close button of your form.

Public Sub DisableCloseButton(ByVal hwnd As IntPtr)

 

Dim hMenu As IntPtr

Dim menuItemCount As Integer

 

hMenu = GetSystemMenu(hwnd, False)

menuItemCount = GetMenuItemCount(hMenu)

 

Call RemoveMenu(hMenu, menuItemCount - 1, _

 

MF_DISABLED Or MF_BYPOSITION)

Call RemoveMenu(hMenu, menuItemCount - 2, _

 

MF_DISABLED Or MF_BYPOSITION)

Call DrawMenuBar(hwnd)

 

End Sub

 

  • In the click event of “btnClose” write the code to exit from application as below:

'To exit from the application

Application.Exit()

 

·         In the click event of “btnDisable” write call the “DisableCloseButton” function to disable the close button as below:

 

'To call the function by passing the handler of form

DisableCloseButton(Me.Handle)

Article Comments
How do I disable the Min/Max buttons instead of the closed?

Posted on 1/24/2007 12:59:26 PM by Ryan

pls find for artical for disable windows

Posted on 3/5/2007 6:49:12 AM by krishan

This method does not work properly!

I've just tried it in Visual Studio 2005 and I've observerd that this function "RemoveMenu" return this error message : "A call to PInvoke function 'FDL Batch Manager!FDL_Batch_Manager.WinApi::RemoveMenu' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature."

Posted on 3/8/2007 4:48:37 AM by marco

how do i disable explorer window back/forward button in vb.net.

Posted on 4/10/2007 5:24:19 AM by pradeep

how do i disable explorer window back/forward button in vb.net.

Posted on 4/10/2007 5:24:33 AM by pradeep

please help me. i want create cristal repot in vb.net

Posted on 4/12/2007 11:28:35 PM by pradeep

please help me. i want create cristal repot in vb.net

Posted on 4/12/2007 11:28:49 PM by pradeep

how to create inbox and mail sending and atachment in vb.net

Posted on 4/12/2007 11:32:55 PM by pradeep

This code does work, but if you want to save yourself some headache, look for the ControlBox in the form's properties list. Setting this to false will hide the Min/Max buttons and the Close button but will retain the form's color bar so the user can still drag the form.

Posted on 5/10/2007 3:24:26 PM by Veers

Look in the properties of the form, you can remove the min and max buttons

Posted on 11/24/2007 7:19:15 PM by Frank

Look in the properties of the form, you can remove the min and max buttons

Posted on 11/24/2007 7:19:16 PM by Frank

Look in the properties of the form, you can remove the min and max buttons

Posted on 11/24/2007 7:19:17 PM by Frank

Private Sub btnDisable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisable.Click
Me.MaximizeBox = False
Me.MinimizeBox = False
End Sub

Posted on 12/15/2007 11:48:19 PM by Eliteazza

Working fine in System menu.

But is it possible to disable the actual restore button?

Posted on 4/10/2008 5:58:02 AM by jasan

It works only in system menu.

but when moving the mouse over the top right corner and giving a click performs the close operation which is meaningless

can u provide another soln for this?

Posted on 4/10/2008 6:07:33 AM by Jasan

Does any one know how to change the function of the close button?

I want to show the close button, but instead of just closing the app, i 'd like to write the time it was closed to a text file.
I know how to the writing bit, just how do get the close button to do what i want?

Any one know please?

Posted on 6/16/2008 9:21:05 AM by Steej

Very nice and expert script. Thank you very much for my new experience.

Posted on 3/12/2009 3:21:48 AM by Keetawat

Buttons in upper right corner:
To remove the minimum, maximum, and close buttons: In the Properities window for the form set ControlBox to false.

To remove minimum button: In the Properities window for the form set MinimizeBox to false

To remove maximum button: In the Properities window for the form set MaximizeBox to false

Posted on 7/28/2010 2:28:14 AM by Robert M. Politi

Thank you DevASP! I used your logic and built it into my own app to suit my needs to prevent MDI child forms from being closed while maintaining min and max form functions. Works great and for those who are having problems with the code... you may need to review your own logic. I had to debug a time or 2 in order for it to function as intended.

Kudos once again author! Keep up the good work!!

Posted on 11/8/2010 5:10:47 PM by Paul Kalverda

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to Create Media Player in VB.NET

Disclaimer - Privacy
© 2002-2014 DevASP.net