i want to my application from closing when the user presses the "X" button on the form, what is the event for that
Printable View
i want to my application from closing when the user presses the "X" button on the form, what is the event for that
I think you mistyped. Are you saying you want to stop your app from closing if the user clicks the Close button in the title bar? If so, you need to set a variable when the user selects a legal closing method. You then check that variable in the form's Closing event handler. If the variable is set then you do nothing and the app closes. If the variable is not set, set the Cancel property of the CancelEventArgs argument to True and the app will not close.
i found it myself... heres the code
VB Code:
Const WM_ENDSESSION As Integer = &H16 Dim osexit As Boolean = False Protected Overrides Sub WndProc(ByRef e As Message) If (e.Msg = WM_ENDSESSION) Then osexit = True Application.Exit() End If MyBase.WndProc(e) End Sub Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing If osexit Then 'Not doing anything will let the application close Else Dim sure sure = MsgBox("Are you sure you want to terminate the application", MsgBoxStyle.Critical + MsgBoxStyle.YesNo) If sure = vbYes Then Application.Exit() Else e.Cancel = True 'This will cause the application to ignore the close & continue running Me.Hide() End If End If End Sub
Why have you over-ridden the wndProc procedure? Could you care to explain?
When you call Application.Exit the app shuts down without raising any Form.Closing events, so setting osexit and then testing for it in the Closing event handler is pointless. Whenever it is set to true the Closing event handler will not be called anyway.
There is one more gap.
I ran the application. I shut down windows. The application prompted me for exit. I clicked on No. The windows shut-down process was aborted, but the application got closed. Could you tell me why is that happening?
My guess is that the form Closing event handler is called, you are prompted, you say no so the app shutdown is aborted and thus the Windows shutdown is aborted. Then the WM_ENDSESSION message is processed and Application.Exit is called, which exits the app without raising the Closing event and thus you don't get prompted. I don't know that this is what happens for a fact, but it seems likely. hyuosuf2, what exactly is it that you are trying to achieve and we may be able to find a bug free way to do it? It is not clear from your original post or your code.Quote:
Originally Posted by abhijit
I tried putting some code over here. For windows shut-down, but that did not work.
VB Code:
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing If osexit Then 'Not doing anything will let the application close e.Cancel = True 'A windows shutdown will not shut down the app. Else Dim sure sure = MsgBox("Are you sure you want to terminate the application", MsgBoxStyle.Critical + MsgBoxStyle.YesNo) If sure = vbYes Then Application.Exit() Else e.Cancel = True 'This will cause the application to ignore the close & continue running 'Me.Hide() End If End If End Sub
Help,
Abhijit
Like I said, that code will never be executed. The only place that osexit gets set to True calls Application.Exit on the next line. Application.Exit shuts down the app without raising any Form.Closing events, so the event handler containing your new bit of code is not called.Quote:
Originally Posted by abhijit
So in that case, how do u prevent your application from closing up?
Remove the call to Application.Exit(). Note that once you have tested osexit and found it to be True, you must then set it back to False or you will not be prompted on any subsequent attempts to close, i.e.Quote:
Originally Posted by abhijit
VB Code:
If osexit Then e.Cancel = True osexit = False