|
-
Jun 15th, 2005, 05:32 AM
#1
Thread Starter
Addicted Member
Form_QueryUnload in vb.net
i want to my application from closing when the user presses the "X" button on the form, what is the event for that
Women ...r like tea bags, you neva know how strong they really r untill u put them in hot water
Huzefa Yousuf
Software Engineer
Verticity Inc.
+92-345-2235303
[email protected]
-
Jun 15th, 2005, 05:40 AM
#2
Re: Form_QueryUnload in vb.net
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.
-
Jun 15th, 2005, 05:45 AM
#3
Thread Starter
Addicted Member
Re: Form_QueryUnload in vb.net
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
Women ...r like tea bags, you neva know how strong they really r untill u put them in hot water
Huzefa Yousuf
Software Engineer
Verticity Inc.
+92-345-2235303
[email protected]
-
Jun 16th, 2005, 03:19 AM
#4
Re: Form_QueryUnload in vb.net
Why have you over-ridden the wndProc procedure? Could you care to explain?
Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
save a blobFileStreamDataTable To Text Filemy blog
-
Jun 16th, 2005, 03:25 AM
#5
Re: Form_QueryUnload in vb.net
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.
-
Jun 16th, 2005, 03:31 AM
#6
Re: Form_QueryUnload in vb.net
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?
Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
save a blobFileStreamDataTable To Text Filemy blog
-
Jun 16th, 2005, 03:46 AM
#7
Re: Form_QueryUnload in vb.net
 Originally Posted by abhijit
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.
-
Jun 16th, 2005, 04:14 AM
#8
Re: Form_QueryUnload in vb.net
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
Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
save a blobFileStreamDataTable To Text Filemy blog
-
Jun 16th, 2005, 06:37 AM
#9
Re: Form_QueryUnload in vb.net
 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.
-
Jun 16th, 2005, 07:32 AM
#10
Re: Form_QueryUnload in vb.net
So in that case, how do u prevent your application from closing up?
Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
save a blobFileStreamDataTable To Text Filemy blog
-
Jun 16th, 2005, 07:38 AM
#11
Re: Form_QueryUnload in vb.net
 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.
VB Code:
If osexit Then
e.Cancel = True
osexit = False
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|