Results 1 to 8 of 8

Thread: How can I disable a close button on a form?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2007
    Posts
    788

    How can I disable a close button on a form?

    How can I disable a close button which is on upper right corner of a form as show in the attached file?
    Attached Images Attached Images  
    Last edited by VBLearner6; Oct 2nd, 2008 at 04:55 AM.

  2. #2
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: How can I disable a close button on a form?

    You can't directly.

    Most people just trap it in the Query_Unload event handler.

  3. #3
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: How can I disable a close button on a form?

    I'm not familiar with Query_Unload but here is how to trap a number of close events:

    Code:
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    
        Select Case UnloadMode
            Case vbFormControlMenu
                MsgBox "The user chose the Close command (the ""X"") from the Control menu on the form."
            Case vbFormCode
                MsgBox "The Unload statement is invoked from code."
            Case vbAppWindows
                MsgBox "The current Microsoft Windows operating environment session is ending."
            Case vbAppTaskManager
                MsgBox "The Microsoft Windows Task Manager is closing the application."
            Case vbFormMDIForm
                MsgBox "An MDI child form is closing because the MDI form is closing."
            Case vbFormOwner
                MsgBox "A form is closing because its owner is closing."
        End Select
    
    End Sub

  4. #4
    Lively Member
    Join Date
    Jul 2007
    Posts
    98

    Re: How can I disable a close button on a form?

    just add Cancel= 1 in the case statement you want to disable

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How can I disable a close button on a form?

    Is this what you are looking for?

    http://www.codeguru.com/vb/gen/vb_fo...icle.php/c3031

    Edit

    There is one more simpler way to do it...

    Paste this in the form code

    Code:
    Private Sub Form_Load()
        Temp = DisableCloseButton(Form1) = False ' where Form1 is the name of your form
    End Sub
    and paste this in a module

    Code:
    Private Declare Function RemoveMenu Lib "user32" _
        (ByVal hMenu As Long, _
         ByVal nPosition As Long, _
         ByVal wFlags As Long) As Long
    Private Const MF_BYPOSITION = &H400&
    
    Public Function DisableCloseButton(frm As Form) As Boolean
        Dim lHndSysMenu As Long
        Dim lAns1 As Long, lAns2 As Long
        lHndSysMenu = GetSystemMenu(frm.Hwnd, 0)
        lAns1 = RemoveMenu(lHndSysMenu, 6, MF_BYPOSITION)
        lAns2 = RemoveMenu(lHndSysMenu, 5, MF_BYPOSITION)
        DisableCloseButton = (lAns1 <> 0 And lAns2 <> 0)
    End Function
    Last edited by Siddharth Rout; Oct 2nd, 2008 at 09:48 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How can I disable a close button on a form?

    We have FAQ code as well as mine and many others code which is the same or similar to koolsids. The Search page is your friend

    LOL, damn this is an old post of mine:
    http://www.vbforums.com/showpost.php...71&postcount=2
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    May 2007
    Posts
    788

    Re: How can I disable a close button on a form?

    Quote Originally Posted by RobDog888
    We have FAQ code as well as mine and many others code which is the same or similar to koolsids. The Search page is your friend

    LOL, damn this is an old post of mine:
    http://www.vbforums.com/showpost.php...71&postcount=2
    Thanks. Can I disable Maximise button and Close button like in the following
    attached file:
    Attached Images Attached Images  

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How can I disable a close button on a form?

    Yes, just set the MaxButton property of the form to False.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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