jkh
jkh

Reputation: 3678

Detect when a form has been closed c#

I have a WinForm that I create that shows a prompt with a button. This is a custom WinForm view, as a message box dialog was not sufficient.

I have a background worker started and running. I also want to exit the while(aBackgroundWorker.IsBusy) loop if the button on myForm was clicked.

//MyProgram.cs

using(CustomForm myForm = new CustomForm())
{
    myForm.Show(theFormOwner);
    myForm.Refresh();

    while(aBackgroundWorker.IsBusy)
    {
        Thread.Sleep(1);
        Application.DoEvents();
    }
}

Right now, in the CustomForm the Button_clicked event, I have

//CustomForm.cs

private void theButton_Click(object sender, EventArgs e)
{
  this.Close();
}

Do I need to add more code to the CustomForm class, or the location where I declare and initialize the form in order to be able to detect a closure?

Upvotes: 17

Views: 66784

Answers (7)

dcarl661
dcarl661

Reputation: 322

Note that this.Hide(); is not the same as this.Close(); in the actual dialog your overriding the closed event

Upvotes: 0

Dracorat
Dracorat

Reputation: 1194

You might be going overkill. To show a form like a dialog window and wait for it to exit before returning control back to the calling form, just use:

mySubForm.ShowDialog();

This will "block" the main form until the child is closed.

Upvotes: 6

evasilchenko
evasilchenko

Reputation: 1870

Make sure your background worker supports cancellation and as others have pointed out use the form closed event handler. This code should point you in the right direction:

using(CustomForm myForm = new CustomForm())
{
  myForm.FormClosed += new FormClosedEventHandler(ChildFormClosed);
  myForm.Show(theFormOwner);
  myForm.Refresh();


  while(aBackgroundWorker.IsBusy)
  {
    Thread.Sleep(1);
    Application.DoEvents();
  }
}

void ChildFormClosed(object sender, FormClosedEventArgs e)
{
    aBackgroundWorker.CancelAsync();
}

Upvotes: 1

Sam Axe
Sam Axe

Reputation: 33738

A couple things...

First, it appears that loop is there in order to prevent execution form proceeding while the dialog is open. If that is the case, change you .Show(parent) to .ShowDialog(parent). That will also take care of the rest of your question.

Upvotes: 0

competent_tech
competent_tech

Reputation: 44921

To detect when the form is actually closed, you need to hook the FormClosed event:

    this.FormClosed += new FormClosedEventHandler(Form1_FormClosed);

    void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        // Do something
    }

Alternatively:

using(CustomForm myForm = new CustomForm())
{
    myForm.FormClosed += new FormClosedEventHandler(MyForm_FormClosed);
    ...
}

void MyForm_FormClosed(object sender, FormClosedEventArgs e)
{
    // Do something
}

Upvotes: 34

user1231231412
user1231231412

Reputation: 1659

You should be able to hook into the FormClosing and FormClosed events.

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosed.aspx

Closing is before it's closed. Closed is after it's closed.

Upvotes: 0

Kekoa
Kekoa

Reputation: 28230

Handle the FormClosing event of the form to be notified when the form is closing, so you can perform any cleanup.

Upvotes: 0

Related Questions