VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



This code shows how to disable the X-button on a form, to keep the user from closing a form that wa

by Syed Shariq Rasheed (3 Submissions)
Category: Custom Controls/Forms/Menus
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Wed 3rd January 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)

This code shows how to disable the X-button on a form, to keep the user from closing a form that way. Ok, now I hear you saying that this can

Rate This code shows how to disable the X-button on a form, to keep the user from closing a form that wa




MyForm is the name of the form you want to disable the X-button on.


Put the following code in a bas module
'//*********************************//'
Public Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Public Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As Long
Public Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Public Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long

Public Const MF_BYPOSITION = &H400&
Public Const MF_DISABLED = &H2&

Public Sub DisableX(Frm As Form)
    Dim hMenu As Long, nCount As Long

    'Get handle to system menu
    hMenu = GetSystemMenu(Frm.hwnd, 0)

    'Get number of items in menu
    nCount = GetMenuItemCount(hMenu)

    'Remove last item from system menu (last item is 'Close')
    Call RemoveMenu(hMenu, nCount - 1, MF_DISABLED Or MF_BYPOSITION)

    'Redraw menu
    DrawMenuBar Frm.hwnd

End Sub



Download this snippet    Add to My Saved Code

This code shows how to disable the X-button on a form, to keep the user from closing a form that wa Comments

No comments have been posted about This code shows how to disable the X-button on a form, to keep the user from closing a form that wa. Why not be the first to post a comment about This code shows how to disable the X-button on a form, to keep the user from closing a form that wa.

Post your comment

Subject:
Message:
0/1000 characters