Results 1 to 4 of 4

Thread: Classic VB - How can I Hide/Unhide the Form's "X" Button

Threaded View

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Classic VB - How can I Hide/Unhide the Form's "X" Button

    Credit for the Hide/Unhide code goes to crptcblade.

    VB Code:
    1. Public Declare Function GetSystemMenu Lib "user32" (ByVal hwnd  _
    2. As Long, ByVal bRevert As Long) As Long
    3. Public Declare Function RemoveMenu Lib "user32" (ByVal hMenu _
    4.  As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
    5. Public Declare Function DrawMenuBar Lib "user32" (ByVal hwnd  _
    6. As Long) As Long
    7.  
    8. Private Const MF_BYCOMMAND = &H0&
    9. Private Const SC_CLOSE = &HF060&
    10.  
    11. Public Sub SetXState(frm As Form, blnState As Boolean)
    12.     Dim hMenu As Long
    13.  
    14.     hMenu = GetSystemMenu(frm.hwnd, blnState)
    15.     Call RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND)
    16.     Call DrawMenuBar(frm.hwnd)
    17.  
    18. End Sub

    Example of Usage:
    VB Code:
    1. Private Sub Command1_Click()
    2.     If opt(0).Value = True Then
    3.         SetXState Me, True   'enable the X button
    4.     ElseIf opt(1).Value = True Then
    5.         SetXState Me, False  'disable the X button
    6.     End If
    7. End Sub

    You can however avoid all that and just do this
    VB Code:
    1. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    2.     If UnloadMode = vbFormControlMenu Then
    3.         cmdExit = True ' Change cmdExit to the name of your exit command button
    4.                        ' and the code behind that button will be executed
    5.     End If
    6. End Sub
    Last edited by si_the_geek; Nov 1st, 2005 at 04:30 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width