Page 1 of 2 12 LastLast
Results 1 to 40 of 59

Thread: Disable Close Button and Prevent Form Being Moved

  1. #1

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

    Disable Close Button and Prevent Form Being Moved

    C# version here.

    SystemMenuManager class - A helper class that you can add an instance of to a form and remove the Move item from the system menu, which renders the form immoveable by the user, and/or disable or remove the Close item, which also disables the Close button on the title bar. When the Close item is disabled or removed the Alt+F4 key combination is also disabled. To use this class just add a single line of code similar to this to your form:
    VB.NET Code:
    1. Private myMenuManager As New SystemMenuManager(Me, False, SystemMenuManager.MenuItemState.Greyed)
    Note that it does not affect any other items on the system menu as these should be accessed through properties of the form itself. Also, the Move item can only be removed as disabling it or greying it out seem to have no effect.

    EDIT: I've taken the code from post #39 and incorporated it into a class that I've attached to this post. You can use this class to make your form immovable like so:
    vb.net Code:
    1. Private immobiliser As New FormImmobiliser(Me)
    EDIT (8-Jan-2009): I've updated the code for the FormImmobiliser class so it's now a bit simpler and it also addresses a few issues that the old code had.
    Attached Files Attached Files

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

    Re: Disable Close button

    Nice code John. Good job. Its about time MS added the EnableMenuItem API.

    Probably would be better if it was a prublic function in a class somewhere so you can call it throughout your project.
    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

  3. #3

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

    Re: Disable Close button

    Thanks Rob. You give good rep. I've come from a C++ background so the succincter (sic) the code the better. I love that C code that does about fifty things in one line and there are more dots and arrows than other characters.

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

    Re: Disable Close button

    Quote Originally Posted by jmcilhinney
    Thanks Rob. You give good rep. I've come from a C++ background so the succincter (sic) the code the better. I love that C code that does about fifty things in one line and there are more dots and arrows than other characters.
    I know what you mean. I hate to write many lines of simple code when you can usually nest function calls inside of each other, etc. 's again.
    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

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

    Re: Disable Close button

    Hey, John. What about using MF_DISABLED instead of MF_GRAYED?
    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

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

    Re: Disable Close button

    I tested it both ways and its not the same. The MF_GRAYED disables the 'x' AND the system menu Close item, but the MF_DISABLED constant only disables the 'x' and leaves the Close system menu item looking enabled but is actualy disabled.
    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
    Addicted Member Pythagoras's Avatar
    Join Date
    Feb 2005
    Location
    Greece
    Posts
    137

    Re: Disable Close button

    How about disabling the ALT+F4?
    Visual Studio.Net 2003

    "Every man has been made by God in order to acquire knowledge and contemplate."
    --Pythagoras

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

    Re: Disable Close button

    To disable that keypress sequence I believe you would have to create a WndProc to handle the message stream for that form and intercept the WM_CLOSE message and beable to determine from the lPram and wPram that it is not closing via the code but rather attempting to close from the ALT+F4.
    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

  9. #9
    Hyperactive Member anna7's Avatar
    Join Date
    Sep 2005
    Location
    Catalonia
    Posts
    481

    Talking Re: Disable Close button

    thanks for this code! I needed it


    Bye,

    Anna

  10. #10
    Hyperactive Member anna7's Avatar
    Join Date
    Sep 2005
    Location
    Catalonia
    Posts
    481

    Question Re: Disable Close button

    i have one small problem. When I maximize o minimize the form, close button turns enabled.

  11. #11
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Disable Close button

    Quote Originally Posted by anna7
    i have one small problem. When I maximize o minimize the form, close button turns enabled.
    in the form's ReSize event put jmcilhinney's code in so it'll re-disable the form's titlebar close button

  12. #12
    Hyperactive Member anna7's Avatar
    Join Date
    Sep 2005
    Location
    Catalonia
    Posts
    481

    Re: Disable Close button

    ok! Now run perfectly

  13. #13

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

    Re: Disable Close button

    Attached is a helper class that you can use to affect the Move and Close item on a form's system menu. If you remove the Move item the user cannot move the form by dragging it. If you grey-out, disable or remove the Close item the Close button on the title bar will be disabled and so will the Alt+F4 key combination. To use it you need just one line of code. Just create an instance in your form like this:
    VB Code:
    1. Private menuManager As New SystemMenuManager(Me, False, SystemMenuManager.MenuItemState.Greyed)
    and everything else is done for you. Note that if you disable the Close item then you must provide an alternative method of closing the form. Note also that this class addresses the issue that anna7 picked up earlier. It implements the solution that JuggaloBrotha suggested internally.

    Edit:
    Attachment removed. See post #1.

  14. #14
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Disable Close button

    awsome, thanx jmcilhinney it looks good, i may start using it for some of my app's

  15. #15
    Hyperactive Member nothingofvalue's Avatar
    Join Date
    Jul 2005
    Location
    Arizona
    Posts
    489

    Re: Disable Close Button and Prevent Form Being Moved

    Thanks JMC,
    Excellent and easy to use, even for me.
    "Imagination is more important than knowledge..."

    Albert Einstein
    -----------------------------------------------
    If my reply helped you then you really were lost, but I still took the time to help, please rate it anyway

  16. #16
    New Member
    Join Date
    Sep 2005
    Posts
    8

    Re: Disable Close Button and Prevent Form Being Moved

    Why doesnt my window load center screen with this? Its half off.

  17. #17
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Disable Close Button and Prevent Form Being Moved

    Real clean code. Nice commenting, and works great.

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

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

    Thumbs up Re: Disable Close Button and Prevent Form Being Moved

    I thought I would post this for other users as I found it helpful. If you want to disable the close button of a form that is an MdiChild of another form. That is to say if at run time you have a form that loads inside another form using the

    VB Code:
    1. Me.MdiParent = TheParentOrMainForm.ActiveForm

    Where "TheParentOrMainForm" is the name of the form you want the current form you’re adding this code to, to appear inside of.

    Well to do that firstly add a new class and name it "SystemMenuManager" to your project. Copy the code from the file attachment above in this thread replacing any code already their.

    Then add a timer component to your form from the tools menu

    Add this to you child form in the code

    VB Code:
    1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    2.   Me.MdiParent = TheParentOrMainForm.ActiveForm
    3.         Dim myMenuManager As New SystemMenuManager(Me, False, SystemMenuManager.MenuItemState.Greyed)
    4.     End Sub

    Also if you want to only remove the close button but allow the form to be movable

    set the Flase to True like so

    VB Code:
    1. (Me, True, SystemMenuManager.MenuItemState.Greyed)

    I hope this helps other users. Don’t forget to remove the maximize and minimizes controls in the design view of your form. In the properties window set the MaximizeBox and MinimizesBox properties to false

    This will basically allow you to disable a forms close button using the above topic method even if the form is a MdiChild of another form and allow you to still move the form about

    I hope this makes sense and is easy to follow. And mostly I hope some one finds it as helpful as I did
    Last edited by rabid lemming; Nov 24th, 2005 at 01:59 AM.

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

  19. #19

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

    Re: Disable Close Button and Prevent Form Being Moved

    Hey, hey, hey! Don't be commenting anything out. The second argument to the constructor you're using indicates whether the Move item should be present or not. Just change that argument to True or use the second overload that omits that argument if you want the form to be movable.

    As for the other issue, it is an odd situation. If you create the SystemMenuManager object in the class initialisation, the constructor, the Load event handler or the Activated event handler it doesn't work. It seems that the use of a Timer is the only way, which works even if the Timer has the smallest possible Interval. I'd actually recommend using a System.Timers.Timer object with an Interval of 1 (millisecond) and the AutoReset set to False so the Elapsed event is only raised once. If you do use a System.Windows.Forms.Timer then make sure you call Stop in the Tick event handler or the Timer will continue to raise the Tick event over and over.

  20. #20

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

    Re: Disable Close Button and Prevent Form Being Moved

    Actually, I just tested a bit more and you don't have to use a Timer if you select Disabled or Removed but you do if you select Greyed. Love those idiosynchrasies!

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

    Re: Disable Close Button and Prevent Form Being Moved

    oops sorry jmcilhinney, I have amended my post now. As for the

    AutoReset set to False so the Elapsed event is only raised once
    Do you mean that once the timer has fired once you can disable it and the form should still act as a child form and have the close button disabled ?

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

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

    Re: Disable Close Button and Prevent Form Being Moved

    hummm... I found that even setting it to Disabled or Removed and the form will be contained with in the main form container but the close button is still visible ?

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

  23. #23

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

    Re: Disable Close Button and Prevent Form Being Moved

    You only have to Disable/Remove the Close menu item once. After that you would have to consciously put it back for it to be available again. There's no need to keep doing it over and over, unless there is some other quirky MDI behaviour that I don't know about. If you allow your Timer to keep raising its Tick event then you are wasting resources on it. The AutoReset property is specifically designed to differentiate between a once only Timer and a continuous one, although it is only a member of the Timers.Timer and not the Windows.Forms.Timer. If you use that one you have to explicitly Stop it. The Timers.Timer is on the Components tab of the Toolbox.

  24. #24

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

    Re: Disable Close Button and Prevent Form Being Moved

    Quote Originally Posted by rabid lemming
    hummm... I found that even setting it to Disabled or Removed and the form will be contained with in the main form container but the close button is still visible ?
    Create the SystemMenuManager in the Load event handler and it should work, as it did for me. If you declare the SMM at the class-level and create it on the same line it will not work.

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

    Talking Re: Disable Close Button and Prevent Form Being Moved

    Yer i got it working using :

    VB Code:
    1. Private Sub LoadTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadTimer.Tick
    2.         Me.MdiParent = PhoneExchange.ActiveForm
    3.         ' this is my own bug fix :
    4.         Me.Left = Me.Left + 1
    5.         LoadTimer.Stop()
    6.     End Sub

    As I found the form close button didn’t disable until you moved the form by dragging it. So I made it move automatically with the timer on tick event

    Thanks for all your help again

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

  26. #26

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

    Re: Disable Close Button and Prevent Form Being Moved

    All I did was put this code in the form's Load event handler:
    VB Code:
    1. Dim smm As New SystemMenuManager(Me, SystemMenuManager.MenuItemState.Removed)
    Nothing else was required. Also, I don't understand why you're setting the form's MdiParent property. The normal thing to do is set the parent from the outside before you call Show. A form setting it's own parent seems very odd.

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

    Cool Re: Disable Close Button and Prevent Form Being Moved

    lol now i just feel silly . You are of course absolutely right, So sorry...I have been up 24 hours so that’s my only excuse thanks for putting me right and putting up with my ignorance

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

  28. #28
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Question Re: Disable Close Button and Prevent Form Being Moved

    What about This

  29. #29

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

    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.

  30. #30
    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

  31. #31

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

    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.

  32. #32
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    685

    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.

  33. #33
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    685

    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.

  34. #34
    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

  35. #35
    Junior Member
    Join Date
    Nov 2007
    Posts
    24

    Re: Disable Close Button and Prevent Form Being Moved

    should i use imports?

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

    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

  37. #37

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

    Re: Disable Close Button and Prevent Form Being Moved

    Quote Originally Posted by kalboako1987
    should i use imports?
    What does this mean?

  38. #38
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: Disable Close Button and Prevent Form Being Moved

    Sorry to refresh an old thread, but I have found a simple (yet far from ideal solution) to blocking the Alt + F4 key combination. It's sloppy, but extremely easy to do. Just set a menu item's shortcut key combination to Alt + F4 and every press will fire the menu item's Clicked event instead of closing the form. Tested and works perfectly.

  39. #39

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

    Re: Disable Close Button and Prevent Form Being Moved

    It's come to my attention that this class will not prevent the form moving if the ControlBox property is set to False. The following code seems to work in all cases:
    vb.net Code:
    1. Private Structure WINDOWPOS
    2.     Public hwnd As Int32
    3.     Public hWndInsertAfter As Int32
    4.     Public x As Int32
    5.     Public y As Int32
    6.     Public cx As Int32
    7.     Public cy As Int32
    8.     Public flags As Int32
    9. End Structure
    10.  
    11. Const WM_WINDOWPOSCHANGING As Int32 = &H46
    12.  
    13. Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    14.     If m.Msg = WM_WINDOWPOSCHANGING Then
    15.         Dim pos As WINDOWPOS = DirectCast(Runtime.InteropServices.Marshal.PtrToStructure(m.LParam, _
    16.                                                                                          GetType(WINDOWPOS)), _
    17.                                           WINDOWPOS)
    18.  
    19.         pos.x = Me.Left
    20.         pos.y = Me.Top
    21.  
    22.         Runtime.InteropServices.Marshal.StructureToPtr(pos, m.LParam, True)
    23.     End If
    24.  
    25.     MyBase.WndProc(m)
    26. End Sub
    See the first post for a downloadable class that incorporates this code.

  40. #40
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,732

    Re: Disable Close Button and Prevent Form Being Moved

    Hi jm!

    Your work is great it helps me alot and I implemented it in my apps.

    I have a main form (not MDI) a treeview menu when user clicked a certain menu it opens another form on top of the main form. Whenever i click the icon of the form found in the start bar the form will minimized but when I click it again I was expecting that it would display it back but it is not restoring. I even tried right click then maximize or restore and its not working. Is it possible that whenever I click the icon in the start bar it will not minimize?
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

Page 1 of 2 12 LastLast

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