Prevent a user from closing a Form
Posted by: Suprotim Agarwal ,
on 11/23/2007,
in
Category WinForms & WinRT
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.!!
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and
The Absolutely Awesome jQuery CookBook.
Suprotim received the prestigious Microsoft MVP award for 17 consecutive years, until he resigned from the program in 2025. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn