Visual Basic – Disable the Close X Button on a Form

--> (Word) --> (PDF) --> (Epub)
This article has been published [fromdate]

Version Compatibility: Visual Basic 6, Visual Basic 5

Instructions: Copy the declarations and code below and paste directly into your VB Project.

Code

  • Declarations (Put this on top, as a best practice, of your VB Project)
Option Explicit

Private Declare Function GetSystemMenu Lib "user32" _
    (ByVal hwnd As Long, _
     ByVal bRevert As Long) As Long

Private Declare Function RemoveMenu Lib "user32" _
    (ByVal hMenu As Long, _
     ByVal nPosition As Long, _
     ByVal wFlags As Long) As Long
     
Private Const MF_BYPOSITION = &H400&
  • Function (Copy & Paste, that’s all)
Public Function DisableCloseButton(frm As Form) As Boolean

'PURPOSE: Removes X button from a form
'EXAMPLE: DisableCloseButton Me
'RETURNS: True if successful, false otherwise
'NOTES:   Also removes Exit Item from
'         Control Box Menu


    Dim lHndSysMenu As Long
    Dim lAns1 As Long, lAns2 As Long
    
    
    lHndSysMenu = GetSystemMenu(frm.hwnd, 0)

    'remove close button
    lAns1 = RemoveMenu(lHndSysMenu, 6, MF_BYPOSITION)

   'Remove seperator bar
    lAns2 = RemoveMenu(lHndSysMenu, 5, MF_BYPOSITION)
    
    'Return True if both calls were successful
    DisableCloseButton = (lAns1 <> 0 And lAns2 <> 0)

End Function
SOURCE

LINK (Freevbcode.com)

LANGUAGE
ENGLISH

1 thought on “Visual Basic – Disable the Close X Button on a Form”

Comments are closed.