CodeItBetter Programming Another VB Programming Blog

How to Enable/Disable Close Button in a form

Posted on August 23, 2011
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
'Forms - How to Enable/Disable Close Button in a form
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 Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long
 
Private Const MF_BYCOMMAND = &H0&
Private Const SC_CLOSE = &HF060&
 
Public Sub SetXState(frm As Form, blnState As Boolean)
    Dim hMenu As Long
    hMenu = GetSystemMenu(frm.hwnd, blnState)
    Call RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND)
    Call DrawMenuBar(frm.hwnd)
End Sub
 
Private Sub Command1_Click()
    'To Disable X button in a form
    Call SetXState(Me, False)
 
    'To Enable X button in a form
    Call SetXState(Me, True)
End Sub
Filed under: Forms Leave a comment
Comments (1) Trackbacks (0)
  1. thank you very much, it works


Leave a comment


 

No trackbacks yet.