How To Stop A .NET Form from Closing

Sharing is Caring

I always find it so amazing how people don’t or won’t bother to spend even five minutes using a search engine like Google to find out about how to do something and instead spend more time posting a question on Yahoo! Answers.

We simply add e.Cancel = true to the Form’s Closing Event, there are two simple examples below.

Closing a Form in C#.NET goes like this:

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
e.Cancel = true

Closing a Form in VB.NET goes like this:

Private Sub Form1_Closing(ByVal sender As Object, ByVal e AsSystem.ComponentModel.CancelEventArgs)
e.Cancel = True
End Sub

Sharing is Caring

Brian is a software architect and technology leader living in Niagara Falls with 13+ years of development experience. He is passionate about automation, business process re-engineering, and building a better tomorrow.

Brian is a proud father of four: two boys, and two girls and has been happily married to Crystal for more than ten years. From time to time, Brian may post about his faith, his family, and definitely about technology.