I would like to add a "confirm exit?" message box when I press the Close(X) button at the top right of my form.
So I try to modify the following code found at the Region " Windows Form Designer generated code ":
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
But I failed. What I one is:
When I press the Close(X) button, the form is still open, and a message box will pop up asking whether you are sure to exit?
If Yes, the form will be dispose.
If No, no action is taken, and the form is still remain there.
How can I do so?
Please guide. Thank you.
Last edited by albertlse; Aug 21st, 2003 at 08:33 PM.
I wouldn't recommend overloading Dispose, rather I suggest you use the .Closing event:
ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemWindowsFormsFormClassClosingTopic.htm
"The Closing event occurs as the form is being closed. When a form is closed, all resources created within the object are released and the form is disposed. If you cancel this event, the form remains opened. To cancel the closure of a form, set the Cancel property of the CancelEventArgs passed to your event handler to true."
I go thru the help file u mentioned, but i not sure whether can implement in my problem, n i dont know how
I have a menu File -> Exit
When I click File, then Exit, to terminate the program, it will show a confirmation dialog box.
The code I implemented is as below:
Private Sub mnuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click
If MessageBox.Show("Are you sure to exit?", Me.Text, _
MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
'Exit this application
Application.Exit()
End If
End Sub
How can I make the Close(X) button on the top right corner of the program to also call the above function before terminating the program? So that I will also get the confirmation dialog box when I press the close(X) button?
That works, although I don't understand why, I thought it should be something more like:
Code:
Private Sub Form1_Closing(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles MyBase.Closing
If frm2.ShowDialog(Me) = DialogResult.Yes Then
e.Cancel = True
End If
End Sub
Why does it work without the If? Actually the above does NOT work. Huh?
As your code explain ,this way it won't work .When you close the form by the upper X then you should first show the form and handle handle the closing event . But in your above code , you are checking if the form already shown and then handle the event (cancel it ) .
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Dispose(True)
Application.Exit()
End Sub
If your app has only one form, or if the app only has one form open at the time the above code is run, then it will work, although Application.Exit (your suggestion) is better if there is a chance multiple forms might be open.
Thank you for your example! It is exactly what I want.
Now I have the idea. Thank you.
I wrote my code like this:
'Close form by clicking the Cancel button
Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
'Dispose form
disposeForm()
End Sub
'Close form by clicking Close button
Private Sub frmEntry_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
e.Cancel = True
'Dispose form
disposeForm()
End Sub
'Dispose form
Private Sub disposeForm()
If MessageBox.Show("Are you sure to cancel??", Me.Text, _
MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
'Dispose form
Me.Dispose()
End If
End Sub
If I press the cmdCancel button, it will call the disposeForm(), and then followed by frmEntry_Closing(...)
It means the confirmation dialog box will pop up twice when I press the cmdCancel button. How to make it once only?
Can we call the frmEntry_Closing(...) explicitly? what will be the parameters? how to set?