devxlogo

Nix the X

Nix the X

Sometimes, you want to show a form that you don’t want users to be able to cancel by clicking on the X-it might not make sense for your app. The best VB solution is to cancel the unload in the form’s QueryUnload event. However, this allows users to do something wrong, for which you then have to handle and scold them. If you do nothing, it looks as if the form has a bug and won’t cancel. Add this routine to a standard BAS module:

 Private Declare Function GetSystemMenu Lib "user32" _	(ByVal hWnd As Long, ByVal bRevert As Long) As LongPrivate Declare Function RemoveMenu Lib "user32" _	(ByVal hMenu As Long, ByVal nPosition As Long, _	ByVal wFlags As Long) As LongPrivate Const MF_BYPOSITION = &H400&Public Sub RemoveCancelMenuItem(frm As Form)	Dim hSysMenu As Long	'get the system menu for this form	hSysMenu = GetSystemMenu(frm.hWnd, 0)	'remove the close item	Call RemoveMenu(hSysMenu, 6, MF_BYPOSITION)	'remove the separator that was over the close item	Call RemoveMenu(hSysMenu, 5, MF_BYPOSITION)End Sub

Then call the routine from any form as it loads:

 	Private Sub Form_Load()		RemoveCancelMenuItem Me	End Sub

After this call, the Close menu item in the system menu and the option to cancel [X] will be disabled. Note that if you’re doing other things with the system menu, you might have to adjust the position number in the RemoveMenu call.

See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist