Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

Working with Window Form Closing Event in C# and VB using .NET 2.0

Author: Zunnair
Download Source Code :

In this simple article you will learn that how to windows form closing event

Simply just create New Project of windows application in C#.


Drag 1 buttons and menu strip on the form.


Now write following code on form Load




private void Form1_Load(object sender, EventArgs e)


            this.Text = "DevAsp Form Closing Event Handling APP";

            button1.Text = "Close";





Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

        Me.Text = "DevAsp Form Closing Event Handling APP"

        button1.Text = "Close"

    End Sub


Now write following code on following events



private void button1_Click(object sender, EventArgs e)





        private void Form1_FormClosing(object sender, FormClosingEventArgs e)


            DialogResult dlg = MessageBox.Show("You want to close Form?", "Warning", MessageBoxButtons.YesNo);

            if (dlg == DialogResult.No)

                e.Cancel = true;

            else if (dlg == DialogResult.No)

                e.Cancel = false;




        private void exitToolStripMenuItem_Click(object sender, EventArgs e)







Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)


    End Sub


    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)

        Dim dlg As DialogResult = MessageBox.Show("You want to close Form?", "Warning", MessageBoxButtons.YesNo)

        If dlg = DialogResult.No Then

            e.Cancel = True

        ElseIf dlg = DialogResult.No Then

            e.Cancel = False

        End If


    End Sub


    Private Sub exitToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)


    End Sub
Article Comments
If you use the e.cancel, the user may not know what to enter into the field and will be stuck there, unable to do anything.
A better way is to display the error message and icon and allow them to leave the control. Then on save or submit, use ValidateChildren() to prevent saving with the error.

Posted on 2/13/2009 12:07:34 PM by Bob Lamberson

I'm using the code exactly as listed, but when I click on the exit botton, it just drops out of the program. I'm working with Visual Studio 2005

Posted on 11/23/2009 11:42:26 AM by Ted

Hi Ted,

Please put try catch block on exit event (Form1_FormClosing) of form so that you will be able to know where the problem is

If you still unable to solve it then let me know


Posted on 11/24/2009 7:10:34 AM by zunnair

Great! This article helped me a lot!

Posted on 5/11/2010 8:44:25 PM by Doug Furbush

Thanks, i' resolve my problem

Posted on 5/28/2010 12:17:04 PM by Frank Cepeda


Posted on 11/9/2010 5:48:14 AM by Ayush Agarwal

Add Article Comment:
Name :
Email Address :
Comments :
<< Working with Combo box Auto Complete Property in C# and VB using .NET 2.0

Disclaimer - Privacy
© 2002-2017