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.
RunBorisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GrahamSkanRetiredCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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 AkbarnejadDeveloperCommented:
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 AkbarnejadDeveloperCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.