Prevent a user from closing a Form

Posted by: Suprotim Agarwal , on 11/23/2007, in Category WinForms & WinRT
Views: 38166
Abstract: In this short snippet, we will learn how easy it is to implement the solution of one of the most frequently asked questions i.e How to prevent a user from closing a form
Prevent a user from closing a Form
 
Follow these steps :
Step 1 : Open Visual Studio 2005. File > New > Project > Choose your desired language (C# or VB) > Choose Windows Application from the Templates window.
Step 2 : Select the Form. In the properties window, click on the lightning like icon to select the events of the form.
Step 3: Find the ‘FormClosing’ event and double click on the event to generate the code.
C#
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
}
VB.NET
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
End Sub
Step 4: Add the following code to prevent the user from closing a form
C#
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
            // Display a MsgBox asking the user to close the form.
            if (MessageBox.Show("Are you sure you want to close the form?", "Close Form",
               MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.No)
            {
                // Cancel the Closing event
                e.Cancel = true;
            }
 
}
VB.NET
 
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
' Display a MsgBox asking the user to close the form.
If MessageBox.Show("Are you sure you want to close the form?", "Close Form", MessageBoxButtons.YesNo,MessageBoxIcon.Question) = DialogResult.No Then
      ' Cancel the Closing event
      e.Cancel = True
End If
 
End Sub
What we are doing over here is that we ask the user if he wants to close the form. If the user says No, we just cancel the closure of a form by setting Cancel property of the CancelEventArgs to true.
That’s it.!!
Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles
Suprotim Agarwal, ASP.NET Architecture MVP, MCSD, MCAD, MCDBA, MCSE, is the CEO of A2Z Knowledge Visuals Pvt. He primarily works as an Architect Consultant and provides consultancy on how to design and develop .NET centric database solutions.

Suprotim is the founder and primary contributor to DotNetCurry, DNC .NET Magazine, SQLServerCurry and DevCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls. and is authoring another one at The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by amal on Sunday, November 25, 2007 2:13 AM
I like it.
Comment posted by Rama Krishna K on Thursday, December 6, 2007 1:57 AM
Good but e.Cancel is not showing in my code i'm using asp.net with c#
Comment posted by Suprotim Agarwal on Thursday, December 6, 2007 5:15 AM
Hi Rama..Hope you are using FormClosingEventArgs?
Comment posted by Sanjeev on Wednesday, January 23, 2008 12:29 AM
this is for Windows Application :D
Comment posted by Josiah Kane on Monday, April 6, 2009 12:12 PM
Is there any way to override this. It sounds a bit dodgy. After all, one could have a virus that simply refuses to close (ain't kind enough to show the "are you sure" box) or some daft programmer like me could make a mistake, and hit debug before implementing a valid way to close the form.
Comment posted by Icy Wind on Tuesday, August 25, 2009 11:52 AM
Ugh.. Josiah, don't know if you still read this but it's extremly simple to
shut down an application with this piece of code in it.

The magic trick? Taskmanager...
Comment posted by mraj on Wednesday, August 25, 2010 5:14 AM
you are using vb.net
Comment posted by jsaladin on Friday, January 7, 2011 5:04 PM
How do I close it later! ??
Comment posted by razieh joulaee on Friday, February 24, 2012 10:54 AM
god help you
good for you
Comment posted by Lloyd on Tuesday, July 31, 2012 8:15 AM
How can I prevent the form from closing if I put it in menu? Let say... File > Exit... and in the Exit Menu  I put "End"... The form will close or not?

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel