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

5

20

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?

Crigger answered 19/5, 2011 at 14:46 Comment(3)
Override the close event that handles this. Of course if this is done then you have stuff memory which just cannot be seen.Poly
possible duplicate of Hiding MDI Child Forms on Close C#Drud
All the answers here are missing the magic sauce. If you just handle the FormClosing event without a special case, you'll never be able to close the application. Whoops! That's probably not what you or the user intended. The code in my answer to the above-linked duplicate question is correct, though, and works without a hitch in both circumstances.Drud
E
47

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.
}
Encratia answered 19/5, 2011 at 14:51 Comment(3)
This is only true if the form was shown using the Show method. If the form is shown with ShowDialog, then it is not automatically disposed.Huntington
I've already applied this to my form, but visual components Dispose functions are called anyway. It only helps, not to close form.Leisured
Another thing to know, you can inspect e.CloseReason to determine if the user or the application called the Form's Close method.Overmaster
U
4

You can cancel the close event and hide instead.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        e.Cancel = true;
        this.Hide();
    }
Umpire answered 19/5, 2011 at 15:0 Comment(2)
This is the opposite order of calls compared to the accepted answer. Does the order matter?Armes
No, the order does not matter.Milburt
O
2

Yes. You can call the form's "Hide" method.

You can also override OnClosed and not call its base implementation; HOWEVER, when you DO want to dispose of the form, this may get in your way.

Oniskey answered 19/5, 2011 at 14:49 Comment(1)
I don't think so. When you override the Closed event, it's too late :)Encratia
A
0

Sure, you can cancel the close and hide it. It doesn't seem like a good thing to do, but you definitely can.

See Form.FormClosing Event (MSDN).

Adiell answered 19/5, 2011 at 14:50 Comment(0)
C
0
    void SaveInfo()
{
blnCanCloseForm = false;
Vosol[] vs = getAdd2DBVosol();
if (DGError.RowCount > 0)
return;

Thread myThread = new Thread(() =>
{
this.Invoke((MethodInvoker)delegate {
    picLoad.Visible = true;
    lblProcces.Text = "Saving ...";
});
int intError = setAdd2DBVsosol(vs);
Action action = (() =>
{
    if (intError > 0)
    {
        objVosolError = objVosolError.Where(c => c != null).ToArray();
        DGError.DataSource = objVosolError;// dtErrorDup.DefaultView;
        DGError.Refresh();
        DGError.Show();
        lblMSG.Text = "Check Errors...";
    }
    else
    {
        MessageBox.Show("Saved All Records...");
        blnCanCloseForm = true;
        this.DialogResult = DialogResult.OK;
        this.Close();
    }

});
this.Invoke((MethodInvoker)delegate {
    picLoad.Visible = false;
    lblProcces.Text = "";
});
this.BeginInvoke(action);
});
myThread.Start();
}

void frmExcellImportInfo_FormClosing(object s, FormClosingEventArgs e)
{
    if (!blnCanCloseForm)
        e.Cancel = true;
}
Cambist answered 23/6, 2017 at 8:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.