Discussion Locked

Disable close/terminate('x) on user form

0 helpful
10 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
How can I disable the close/terminate('x) button on a user form. I do not want my users to terminate a form by clicking the "X" to close the form

10 replies

by stillatit

It depends on the language you are writing in, but in general if it is one of the Visual languages, right-click on the form itself in design mode, select properties, and look for a property named "ControlBox" or similar, and turn it off.

If all else fails, use the language's help package and search for "terminate" or "close" on the search tab.

In Visual Basic 5 the properties are ControlBox, MaxButton and MinButton. Also accessable as object.ControlBox and so on. Note that theBorderStyle property controls what the border looks like, and some BorderStyle's do not all the controls at all.

Good luck.

by bob sellman

You don't mention what you are using to generate your application.

If you are using Access, it's very simple. Look on the properties for the form and find the property labeled "control box". Set control box to No and you will not get the minimize/maximize/close buttons on the form.

Alternately, you can just set the Close button property to No so the user can still resize the form.

If you do this, be sure you put a control on your form that enables the user to close the form, otherwise you will end up with a form that "can't" be closed.

by rrv

The code below is in VB6. you can convert it to any language as you wish to disable X button and remove the 'Close Alt-F4' menu item of a window.

first put the below lines in general declaration or a module.

Public Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long
Public Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As Long
Public Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Public Declare Function DrawMenuBar Lib "user32" (ByVal hWnd As Long) As Long

Public Const BYPOS = &H400&
Public Const REMOVE = &H1000&

write the below code in your form load event or when you want to disable X button.

Dim hMenu As Long
Dim nCount As Long
hMenu = GetSystemMenu(Me.hWnd, 0)
nCount = GetMenuItemCount(hMenu)

Call RemoveMenu(hMenu, nCount - 1, REMOVE Or BYPOS)
Call RemoveMenu(hMenu, nCount - 2, REMOVE Or BYPOS)

DrawMenuBar Me.hWnd

All the best.
rrv@hotvoice.com

by barry evans

As others have said, you don't mention the programming language. If this is VB you can set the ControlBox to False for the form. Alternatively, you could add code to the Form_QueryUnload to set Cancel = True. This aborts the form unload event.

This discussion is closed to new replies.