Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to allow the user to choose whether or not they really want to close the form, in case they accidentally click the X button. I have it where the application asks whether they want to close it, and when they click "Yes", it closes. But, what should I put in the "No" event handler so that it stops the form from closing?

The code that I have so far is in the Form's (Form_Closing) event.
Posted

Try setting Cancel = True in the Closing event to prevent the form from unloading.
 
Share this answer
 
v3
Comments
drummerboy0511 19-Nov-10 22:34pm    
Ahh... I'm unsure of how to do this.
Abhinav S 20-Nov-10 2:48am    
Have a look at http://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventargs.cancel.aspx.
Dalek Dave 20-Nov-10 4:58am    
5 for that, spot on.
Abhinav S 20-Nov-10 11:04am    
Thanks DD.
drummerboy0511 20-Nov-10 11:13am    
Ahh, okay. Thank you very much!
Hi! I have the solution of this Problem

Use This Code :

VB
Private Sub FrmMDI_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    Dim Flag As Integer
    Flag = (MsgBox("Are you sure you want to exit ? ", vbInformation + vbOKCancel, "Exit"))
    If Flag = 2 Then e.Cancel = 1
    End
End Sub


And Your Problem is Solved.
Thank you. :)
 
Share this answer
 
Comments
drummerboy0511 20-Nov-10 11:12am    
Thank you very much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900