Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Feb 3, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 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&

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.
Josh Frank
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date