• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1660
  • Last Modified:

Disable CLOSE button

I want to disable the close button (the X in the top right corner). Either gray it out, or display a message that says something like "Can not manually close window".

This window has some key functions and a timer that will make it close automatically after a short amount of time, thus it should not be close prematurely.
0
RunBoris
Asked:
RunBoris
  • 3
  • 2
  • 2
  • +4
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The following will disable the X in the top right of the form:

Option Explicit

' Disable "X" on window
Private Declare Function DrawMenuBar Lib "User32" (ByVal hWnd As _
  Long) As Long
Private Declare Function GetMenuItemCount Lib "User32" (ByVal hMenu _
  As Long) As Long
Private Declare Function GetSystemMenu Lib "User32" (ByVal hWnd As _
  Long, ByVal bRevert As Long) As Long
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
Private Const MF_REMOVE = &H1000

Private Sub Form_Load()
    ' Disable the "X" on the form
    Dim hMenu As Long
    Dim lItemCount As Long
    hMenu = GetSystemMenu(Me.hWnd, 0)
    If hMenu Then
        lItemCount = GetMenuItemCount(hMenu)
        Call RemoveMenu(hMenu, lItemCount - 1, MF_REMOVE Or _
        MF_BYPOSITION)
        Call RemoveMenu(hMenu, lItemCount - 2, MF_REMOVE Or _
        MF_BYPOSITION)
        Call DrawMenuBar(Me.hWnd)
    End If
End Sub
0
 
RogueSolutionsCommented:
If you don't fancy being so brutal with your window you can put a bit of code in the Form's QueryUnload event that says ...

If UnloadMode = vbFormControlMenu Then
     Cancel=True
     msgbox "Cannot manually close window"    'This is optional, the Cancel bit prevents the close
End If
0
 
GrahamSkanCommented:
This is a bit simpler, and uses VB functionality only

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Select Case vbFormControlMenu
        Case vbFormControlMenu
            MsgBox "Command Close Selected"
            Cancel = True
    End Select
End Sub
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RogueSolutionsCommented:
Actually, if your Form is running on a timer and doesn't need to interact you could just set the Enabled property to False
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I like being "brutal" with my windows...

;-)

~IM
0
 
atheekurrahmanCommented:
Hi,

 I u dont want the close button

just give the border style as none


if u dont want this RogueSolutions is the best

0
 
garyLittleCommented:
I will be ever more simple.
There is a property for the form called "ControlBox", just set it to false, and voila - the X disappers.
0
 
Farzad AkbarnejadCommented:
Hello,
The user expects after pressing the "X" button, the from to be closed. Showing the MessageBox or disabling the entire Form causes his expectation not to happen.

After showing a dialog the user must concentrate on the controls like TextBoxes, Option or Command buttons. Hiding the X button causes that the user thinks he can't close the form so he start to find a way for closing the form.

By see the disabled grayed X button (like dialog of MsgBox function) the user understands that he can't close the form manually and must press OK, Cancel or Abort or Retry button.

I think that the solution of Idle_Mind is the best. It is very user-friendly but needs more code.

Thanks
-FA
0
 
RunBorisAuthor Commented:
Thanks, FA, for your suggestion. However, in my particular instance the accepted answer from RogueSolutions. As stated in my original query:
"This window has some key functions and a timer that will make it close automatically after a short amount of time, thus it should not be close prematurely."

Just to clear up some confusion as to why I need this, here is the basic outline. My application is meant to get my wife off the computer, because she's an addict. It will automatically shutdown the PC and NOT allow access again based on my admin-defined times. This particular form with the disabled X and timer is a "You have 60 seconds to save your game, or else..." form. If the form is closed, the timer is stopped; so the form must stay open. I'm not worried about her using the TaskMan, because she's not that sneaky. And besides, my app keeps a log, so she knows not to try anything sneaky. By the way, if anyone wants a copy of this, just e-mail me. I can also send the source if you have ideas on improvements. runboris*at*carolina*dot*rr*dot*com.
0
 
Farzad AkbarnejadCommented:
Thanks a lot, I am single and my girl firend works with PC a little.

Thanks
-FA
0
 
RogueSolutionsCommented:
RunBoris

Thanks for the points.  

When you wife posts a question about some annoying bit of software that keeps turning her PC off, we'll ignore it.
0
 
RunBorisAuthor Commented:
Haha! Luckily she knows nothing of this site. But just in case, keep an eye out for her!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now