Results 1 to 40 of 59

Thread: Disable Close Button and Prevent Form Being Moved

Hybrid View

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Disable Close Button and Prevent Form Being Moved

    Quote Originally Posted by shakti5385
    What about This
    The drawback with that is that you must inherit your form from that class. My class can be added to any form you like, which makes it a little simpler to use. I'd guess that that author has used the same method of disabling the Move menu item though.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  2. #2
    Addicted Member rabid lemming's Avatar
    Join Date
    Feb 2005
    Posts
    210

    Cool Re: Disable Close Button and Prevent Form Being Moved

    Hey,

    I thought I would post this and see if it helps...a simple way to disable the close button in vb 2005

    Code Code:
    1. Private Const SC_CLOSE As Integer = &HF060
    2.     Private Const MF_ENABLED As Integer = &H0
    3.     Private Const MF_GRAYED As Integer = &H1
    4.     Private Const MF_DISABLED As Integer = &H2
    5.     Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As IntPtr, ByVal revert As Boolean) As IntPtr
    6.     Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As IntPtr, ByVal item As Integer, ByVal enab As Integer) As Boolean
    7.  
    8.     Public Sub EnableExit(ByVal ok As Boolean)
    9.         Try
    10.             Dim hdl As IntPtr = GetSystemMenu(Me.Handle, False)
    11.             Dim arg As Integer = MF_ENABLED
    12.             If Not ok Then arg = MF_DISABLED + MF_GRAYED
    13.             EnableMenuItem(hdl, SC_CLOSE, arg)
    14.         Catch ex As Exception
    15.             MsgBox(ex.Message)
    16.         End Try
    17.    End Sub
    18.  
    19. 'call  EnableExit(False) in your form load event to disable the clode button

    Just thought it might be worth posting this

    I will wait for death with a smile and a big stick

  3. #3

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Disable Close Button and Prevent Form Being Moved

    Quote Originally Posted by rabid lemming
    Hey,

    I thought I would post this and see if it helps...a simple way to disable the close button in vb 2005

    Code Code:
    1. Private Const SC_CLOSE As Integer = &HF060
    2.     Private Const MF_ENABLED As Integer = &H0
    3.     Private Const MF_GRAYED As Integer = &H1
    4.     Private Const MF_DISABLED As Integer = &H2
    5.     Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As IntPtr, ByVal revert As Boolean) As IntPtr
    6.     Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As IntPtr, ByVal item As Integer, ByVal enab As Integer) As Boolean
    7.  
    8.     Public Sub EnableExit(ByVal ok As Boolean)
    9.         Try
    10.             Dim hdl As IntPtr = GetSystemMenu(Me.Handle, False)
    11.             Dim arg As Integer = MF_ENABLED
    12.             If Not ok Then arg = MF_DISABLED + MF_GRAYED
    13.             EnableMenuItem(hdl, SC_CLOSE, arg)
    14.         Catch ex As Exception
    15.             MsgBox(ex.Message)
    16.         End Try
    17.    End Sub
    18.  
    19. 'call  EnableExit(False) in your form load event to disable the clode button

    Just thought it might be worth posting this
    Have you read my code? That's exactly how it works, but I wrapped it in a class that you can reuse instead of having to add that code to every form.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    708

    Re: Disable Close Button and Prevent Form Being Moved

    I had run into that problem before where the button becomes reenabled after resizing the form. I no longer have that problem.

    I think I fixed it by declaring the API PROPERLY!
    Yes it's wrong that way, and many API will fail sometimes without an Alias.
    For example the proper way:
    Code:
    Declare Function apiEnableMenuItem Lib "user32" Alias "EnableMenuItem" (ByVal hMenu As IntPtr, ByVal item As Int32, ByVal enab As Int32) As Boolean
    Where the alias name should be different than the function name.
    I'll see if I can indeed verify that as the reason, unless the IntPtr may also cause a problem.

  5. #5
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    708

    Re: Disable Close Button and Prevent Form Being Moved

    I wasn't able to reproduce the quirk, but my comment still stands for API declares.

  6. #6
    Addicted Member rabid lemming's Avatar
    Join Date
    Feb 2005
    Posts
    210

    Red face Re: Disable Close Button and Prevent Form Being Moved

    Opps sorry jmcilhinney my bad

    I will wait for death with a smile and a big stick

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

    Re: Disable Close Button and Prevent Form Being Moved

    Quote Originally Posted by TTn
    I had run into that problem before where the button becomes reenabled after resizing the form. I no longer have that problem.

    I think I fixed it by declaring the API PROPERLY!
    Yes it's wrong that way, and many API will fail sometimes without an Alias.
    For example the proper way:
    Code:
    Declare Function apiEnableMenuItem Lib "user32" Alias "EnableMenuItem" (ByVal hMenu As IntPtr, ByVal item As Int32, ByVal enab As Int32) As Boolean
    Where the alias name should be different than the function name.
    I'll see if I can indeed verify that as the reason, unless the IntPtr may also cause a problem.
    No, that incorrect as in .NET its most proper to use the dllimport attribute as alias' are not used or allowed as EntryPoint is used.

    Code:
    <System.Runtime.InteropServices.DllImport("User32.dll", CharSet:=Runtime.InteropServices.CharSet.Auto, EntryPoint:="EnableMenuItem")> _
    Private Shared Function EnableMenuItem(ByVal hMenu As IntPtr, ByVal item As Int32, ByVal enab As Int32) As Boolean
    End Function
    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