VB icon

Disable Form Close Button

Email
Submitted on: 1/1/2015 12:54:00 PM
By: Claudio Di Flumeri (from psc cd)  
Level: Beginner
User Rating: By 6 Users
Compatibility: VB.NET
Views: 2001
 
     This code is used to disable the "X" form button (that one top right) in a form

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Disable Form Close Button
// Description:This code is used to disable the "X" form button (that one top right) in a form
// By: Claudio Di Flumeri (from psc cd)
//
// Inputs:The Handle of the form
//**************************************

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
'
 Public Sub DisableCloseButton(ByVal hwnd As IntPtr)
 Dim hMenu As IntPtr
 Dim menuItemCount As Integer
'
 'Obtain the handle to the form's system menu
 hMenu = GetSystemMenu(hwnd, False)
'
 'Obtain the number of items in the menu
 menuItemCount = GetMenuItemCount(hMenu)
'
 'Remove the system menu Close menu item.
 'The menu item is 0-based, so the last
 'item on the menu is menuItemCount - 1
 Call RemoveMenu(hMenu, menuItemCount - 1, _
MF_DISABLED Or MF_BYPOSITION)
'
 'Remove the system menu separator line
 Call RemoveMenu(hMenu, menuItemCount - 2, _
MF_DISABLED Or MF_BYPOSITION)
'
 'Force a redraw of the menu. This
 'refreshes the titlebar, dimming the X
 Call DrawMenuBar(hwnd)
 End Sub
'
'
'------------------- USAGE -------------------
 'Put this in the Load Event of a Form
'	
 DisableCloseButton(Me.Handle)


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.