Results 1 to 20 of 20

Thread: Closing form window??

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    70

    Closing form window??

    Is there a way I can hide a form's window "close" button in order to avoid a user from accidently closing the window and shutting the application down??

    Thanks!!!!

  2. #2
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    This code will not allow the user to unload your form.

    VB Code:
    1. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    2.     Cancel = True
    3. End Sub

    I don't know of a way to temporarily hide the X.

  3. #3
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    CHnage your window Controlbox to false...(this hides all 3 buttons)


    with ControlBox = False...if you clear the field for the form caption...the title bar will disappear
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4
    Frenzied Member Microbasic's Avatar
    Join Date
    Mar 2001
    Posts
    1,402
    Yes, you can:

    VB Code:
    1. Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
    2. Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As Long
    3. Private Declare Function ModifyMenu Lib "user32" Alias "ModifyMenuA" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpString As Any) As Long
    4. Private Const MF_BYPOSITION = &H400&
    5. Private Const MF_DISABLED = &H2&
    6. Private Const MF_GRAYED = &H1&
    7.  
    8. Private Sub Form_Load()
    9.     Dim hM As Long, HMCount As Long
    10.  
    11.     hM = GetSystemMenu(Me.hwnd, False)
    12.     HMCount = GetMenuItemCount(hM)
    13.     ModifyMenu hM, HMCount - 1, MF_BYPOSITION Or MF_GRAYED, 0, "Close"
    14. End Sub

    This will disable the X and the Close option.

    EDIT (+Code): Oops, I can't type today.
    Last edited by Microbasic; Jan 16th, 2003 at 10:06 PM.


    MicroBasic
    Dragon Shadow Trainer

    There is no good or evil in the world...only programmers and fools .

  5. #5
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    Microbasic I thought the same thing but didn't see a point in it since the borderstyle could just be changed at design time.

  6. #6
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    Well...Micro's solution might be better anyway... I just gave the quick/easy way to fix it..But if the Min/Max buttons are still needed. then definately go with the API's
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  7. #7
    Frenzied Member Microbasic's Avatar
    Join Date
    Mar 2001
    Posts
    1,402
    Yeah, but if you change the borders, you get rid of the familiar "WINDOWS" look.

    I mean, you wouldn't have the Title Bar, System Menu, MinMax/"?" Buttons, and size borders (or at least 3D borders)


    MicroBasic
    Dragon Shadow Trainer

    There is no good or evil in the world...only programmers and fools .

  8. #8
    Frenzied Member Microbasic's Avatar
    Join Date
    Mar 2001
    Posts
    1,402
    Originally posted by [LGS]Static
    Well...Micro's solution might be better anyway... I just gave the quick/easy way to fix it..But if the Min/Max buttons are still needed. then definately go with the API's
    Sorry about the Chit chat, but...

    That's interesting. Even after a year of absense from VBForums, people still know to call me "Micro" (especially if they're newer members who probably never knew me before).

    Again, sorry for the digression.


    MicroBasic
    Dragon Shadow Trainer

    There is no good or evil in the world...only programmers and fools .

  9. #9
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    Micor...not sure if you remember me.. Geoff_xrx (I pulled a name change after my "year" of absence)
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  10. #10
    Frenzied Member Microbasic's Avatar
    Join Date
    Mar 2001
    Posts
    1,402
    Oh, OK. I still remember you, Geoff.

    That Lightning VB Avatar should have tipped me off. [oops, OT again]


    MicroBasic
    Dragon Shadow Trainer

    There is no good or evil in the world...only programmers and fools .

  11. #11
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Hi Microbasic, i guest this code also generate the similiar result as yours

    VB Code:
    1. Option Explicit
    2. Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
    3. Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
    4.  
    5. Private Sub Form_Load()
    6.     RemoveMenu GetSystemMenu(Me.hwnd, 0), 6, MF_BYPOSITION
    7. End Sub

  12. #12
    Frenzied Member Microbasic's Avatar
    Join Date
    Mar 2001
    Posts
    1,402
    Yes, but could it also do this?

    VB Code:
    1. 'REENABLES THE X BUTTON
    2.     Dim hM As Long, HMCount As Long
    3.  
    4.     hM = GetSystemMenu(Me.hwnd, False)
    5.     HMCount = GetMenuItemCount(hM)
    6.     ModifyMenu hM, HMCount - 1, MF_BYPOSITION Or &H0, 0, "Close"


    MicroBasic
    Dragon Shadow Trainer

    There is no good or evil in the world...only programmers and fools .

  13. #13
    Frenzied Member Microbasic's Avatar
    Join Date
    Mar 2001
    Posts
    1,402
    Of course, you could always rewrite it as this:

    VB Code:
    1. Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
    2. Private Declare Function ModifyMenu Lib "user32" Alias "ModifyMenuA" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpString As Any) As Long
    3.  
    4. Private Sub Form_Load()
    5.     ModifyMenu GetSystemMenu(Me.hwnd, False), 6, &H401&, 0, "Close"
    6. End Sub


    MicroBasic
    Dragon Shadow Trainer

    There is no good or evil in the world...only programmers and fools .

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    70
    Uuuuh...OK.

    Thanks everyone!! This is exactly what I'm looking for.

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    70
    So, now that I keep users from closing the window accidentally how can I close the form with a command button. Why am I not getting the .unload method?

  16. #16
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    Originally posted by Jason Carden
    So, now that I keep users from closing the window accidentally how can I close the form with a command button. Why am I not getting the .unload method?
    VB Code:
    1. Call GetSystemMenu(Me.hwnd, True)
    2.     Unload Me

  17. #17
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    I you use microbasics code to disable


    all you need is the "Unload Me" statement
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    70
    Well that's what I thought, but it's not working. Could it be b/c I'm using the cancel = true in my form_queryunload event??

  19. #19
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    Yes.

  20. #20
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    Originally posted by Shawn N
    Yes.
    as shawn says..

    Yes.
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

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