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

Author: Brian Cline

Brian is a programmer living in Niagara Falls with ten years of development experience. He is passionate about automation, business process re-engineering, and gamification. For the last six years or so, he has been working with Salesforce and JavaScript.

Related Posts

Sharing is Caring