Link to home
Start Free TrialLog in
Avatar of lirong
lirong

asked on

controling the termination of a form

Hi

How can I disbale user from pressing on the [x] button (on the upper-right side of the form).

Thanks,

Liron Golan
ASKER CERTIFIED SOLUTION
Avatar of Erick37
Erick37
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or:

Option Explicit

Const MF_REMOVE = &H1000&
Const MF_BYPOSITION = &H400&

Private Declare Function GetSystemMenu Lib "user32" _
    (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Private Declare Function GetMenuItemCount Lib "user32" _
    (ByVal hMenu 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 Sub Form_Load()
    Dim hMenu As Long
    Dim menuItemCount As Long
    'Obtain the handle to the form's system menu
    hMenu = GetSystemMenu(Me.hwnd, 0)
    If hMenu <> 0 Then
        '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_REMOVE Or MF_BYPOSITION)
        'Remove the system menu separator line
        Call RemoveMenu(hMenu, menuItemCount - 2, _
            MF_REMOVE Or MF_BYPOSITION)
    End If
End Sub

Private Sub Command1_Click()
    Unload Me
End Sub
Avatar of Ruchi
Ruchi

This is to prevent the user from using the control menu or the close button.

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If UnloadMode = vbFormControlMenu Then Cancel = 1
End Sub
I can also set the form property ControlBox to False