Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent a form object from disposing on close?

I am using an MDIParent Form. When I close its child, the object of the child disposes. Is there a way to set child visibility to false instead of disposing?

like image 544
Touseef Khan Avatar asked May 19 '11 14:05

Touseef Khan


2 Answers

By default, when you close a form, it will be disposed. You have to override the Closing event to prevent it, for example:

// Use this event handler for the FormClosing event.
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
  this.Hide();
  e.Cancel = true; // this cancels the close event.
}
like image 124
Quan Mai Avatar answered Oct 25 '22 21:10

Quan Mai


You can cancel the close event and hide instead.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        e.Cancel = true;
        this.Hide();
    }
like image 40
umilmi81 Avatar answered Oct 25 '22 21:10

umilmi81