Prevent user from closing form but allow form to close at logout

G

Guest

I have added this code in an attempt to prevent the user from closing my
simple 1 form application, but the application now prevents logout and
shutdown. What is the proper way to prevent a user from closing the app but
still have it close gracefully at shutdown/logoff?

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
e.Cancel = True
End Sub

Thanks
 
G

Guest

Imports System.ComponentModel

Class DemoForm
Inherits Form

Private Const WM_QUERYENDSESSION = &H11

Private canExit As Boolean
Protected Overrides Sub OnClosing(ByVal e As CancelEventArgs)
e.Cancel = Not canExit
End Sub

Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_QUERYENDSESSION Then
canExit = True
End If
MyBase.WndProc(m)
End Sub

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top