Form Closing Event in C# (Windows Application)

Sometimes, Developers don’t want Users to close or leave a Form right away as Application still has some back ground process going on or it requires a confirmation before closing. In C# Windows Application, you can code at FormClosing Event. Have a look at this code snippet.

private void MailUI_FormClosing(object sender, FormClosingEventArgs e)
    if (backgroundWorkerMailOne.IsBusy)
       MessageBox.Show("Mail has not been sent yet");
       e.Cancel = true;

This will generate a Message if the Background Worker is busy. You may even ask for confirmation at this Event.

// this will restrict user from closing the Form
e.Cancel = true;

// this one will allow user to close the Form
e.Cancel = false;

A Rahim Khan

  1. A+ would read again

    • Ilkin
    • March 23rd, 2011

    Hi, how can ask a question?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: