help me please not close application

sean123voth

Member
Joined
Jun 6, 2006
Messages
5
Location
AZ
Programming Experience
Beginner
:confused: is there a way to when u click on (X) to close the program it willl not close
so then they have to click on the button "close":confused:
 
Here you go. This bit of code will disable the close button if you call the 'disable' sub....

VB.NET:
<DllImport("User32.dll"> _
Private Shared Function GetSystemMenu (ByVal hwnd As Integer, ByVal revert As Integer) As Integer
End Sub
 
<DllImport("User32.dll")> _
Private Shared Function EnableMenuItem(ByVal menu As Integer, ByVal ideEnableItem As Integer, ByVal enable As Integer) As Integer
end sub
 
Private Const SC_CLOSE As Integer = &HF060
Private Const MF_BYCOMMAND As Integer = &H0
Private Const MF_GRAYED As Integer = &H1
Private Const MF_ENABLED As Integer = &H0
 
Public Shared Sub Disable(ByVal form As System.Windows.Forms.Form)
Select Case EnableMenuItem(GetSystemMenu(form.Handle.ToInt32, 0), SC_CLOSE, MF_BYCOMMAND Or MF_GRAYED)
Case MF_ENABLED
Case MF_GRAYED
Case &HFFFFFFFF
Throw New Exception("The Close menu item does not exist.")
Case Else
End Select
End Sub

Just call the 'disable' sub and pass it the form you want to disable the close button of.

Edit: Dont forget to import the following namespace...

VB.NET:
Imports.System.Runtime.InteropServices

Interesting thing though, i couldn't get it to work with the minimize or maximze button.
 
Last edited:
in the form's closing event set e.Cancel to True and the app wont close

you can check variables in the closing event to determin if the app should close or remain running
 
Back
Top