CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2007
    Posts
    11

    Disable "X" button on forms in VB6

    I tried all the examples on this forum to do this but none seemed to work - the "X" botton still functioned. My big issue is that I create some temp files for WAV and AVI data and want to kill the files when the program closes. With exit buttons or menu selection my code executes fine and all files are eliminated but with the "X" button the clean-up is not done.

    Thus I need a very short & consice method of either:

    1.) Disabling the "X" button

    OR

    2.) So mehow executing the cleanup routine when "X" is pressed.

    Thanks for any help you can give...again please do not redirect me to existing code links since they did not work...I already tried them.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Disable "X" button on forms in VB6

    Add this and it won't matter.

    Code:
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
      Call MyExit
    End Sub

    If they click the X, your code will fire.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Nov 2004
    Location
    LA. California Raiders #1 AKA: Gangsta Yoda™
    Posts
    616

    Re: Disable "X" button on forms in VB6

    This will disable the 'x' close button by removing the system menu Close item.
    Also removes the menu separator for asthetics.

    Always provide a way for the form to be closed.

    Code:
    Option Explicit
    'WRITTEN BY ROBDOG888
    Private Declare Function RemoveMenu Lib "user32" ( _
                        ByVal hMenu As Long, _
                        ByVal nPosition As Long, _
                        ByVal wFlags As Long) As Long
    
    Private Declare Function GetSystemMenu Lib "user32" ( _
                        ByVal hwnd As Long, _
                        ByVal bRevert As Long) As Long
    
    Private Declare Function GetMenuItemCount Lib "user32.dll" ( _
                        ByVal hMenu As Long) As Long
    
    Private Const MF_BYPOSITION = &H400&
    
    Private Sub Form_Load()
        'REMOVE THE SYSTEM MENU ITEM - CLOSE
        RemoveMenu GetSystemMenu(Me.hwnd, 0), GetMenuItemCount(GetSystemMenu(Me.hwnd, 0)) - 1, MF_BYPOSITION
        'REMOVE THE MENU SEPARATOR
        RemoveMenu GetSystemMenu(Me.hwnd, 0), GetMenuItemCount(GetSystemMenu(Me.hwnd, 0)) - 1, MF_BYPOSITION
    End Sub
    
    Private Sub Command1_Click()
        Unload Me
    End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda™)
    VB Forums - Super Moderator 2001-Present

    Microsoft MVP 2006-2011

    Please use [code]your code goes in here[/code] tags when posting code.

    Senior Software Engineer MCP, BSEE, CET
    VS 2012 Premium, VS 6.0 Enterprise SP6, VSTO, Office Ultimate 2010, Windows 7 Ultimate
    Star Wars Gangsta Rap SE Reputations & Rating Posts Office Primary Interop AssembliesAdvanced VB/Office Guru™ Word SpellChecker™.NETAdvanced VB/Office Guru™ Word SpellChecker™ VB6Outlook Global Address ListVB6/Crystal Report Ex.VB6/CR Print Setup Dialog Ex.

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

Featured