Disposing a form from parent form in C#?

301

Solution 1

If the two forms doesn't have a parent-dialog type of relationship, you might just want to hook into the Disposed event on the subform to get notified when it closes.

public partial class Form1 : Form
{
    private Form2 _Form2;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (_Form2 != null)
            _Form2.Dispose();

        _Form2 = new Form2();
        _Form2.Disposed += delegate
        {
            _Form2.Dispose();
            _Form2 = null;
        };
        _Form2.Show();
    }
}

Then all you have to do in Form2 is simply to close it:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Close();
    }
}

Solution 2

MSDN docs on disposing of forms:

Dispose will be called automatically if the form is shown using the Show method. If another method such as ShowDialog is used, or the form is never shown at all, you must call Dispose yourself within your application.

Source

On closing vs. disposing:

When a form is closed, all resources created within the object are closed and the form is disposed. You can prevent the closing of a form at run time by handling the Closing event and setting the Cancel property of the CancelEventArgs passed as a parameter to your event handler. If the form you are closing is the startup form of your application, your application ends.

The two conditions when a form is not disposed on Close is when (1) it is part of a multiple-document interface (MDI) application, and the form is not visible; and (2) you have displayed the form using ShowDialog. In these cases, you will need to call Dispose manually to mark all of the form's controls for garbage collection.

Solution 3

  1. You don't need to explicitly call Dispose on the form, the garbage collector will do that for you.

  2. If you want something specific to happen when Form2 "goes away", you can hook into it's form closing event.

EDIT :

On Form2, in the button click, try

this->Close();

That will close that instance of form2 (the form will disappear). If form1 still has a reference to form2, then form2 will not be picked up by the garbage collector, and the GC will not dispose of it.

If there is a reason for form1 to keep a reference to form2 ?

If so, form1 should handle from2's closing event, then form1 can release it's reference to form2 (set it to null).

Now the GC will pickup form2 as a candidate to be collected, it will (in possibly more than one step) call it's Dispose method and free up Form2's memory.

Solution 4

You are not really a reader right? Lot of answers here already.

Edit I want to close(dispose explicitely) form2 object which is created in form1 class when button on form2 is clicked. This edit is to give some more clarity.

If you use ShowDialog then form2 returns when you call close(). So in Form1:

private void button1_Click(object sender, System.EventArgs e)
{
    Form2 oForm2 = new Form2();
    oForm2.MyParentForm = this;
    if (oForm2.ShowDialog() == DialogResult.OK)
    {
        oForm2.Dispose(); //or oForm2.Close() what you want
    }
}

And then call Close() in form2.

Share:
301
M. Talha Bin Asif
Author by

M. Talha Bin Asif

Updated on June 27, 2022

Comments

  • M. Talha Bin Asif
    M. Talha Bin Asif almost 2 years

    I want to forecast the hourly rate of electricity using ARIMA. But, I won't be able to set the ts function correctly which results in no forecasting in Arima. My data has 24 entries per day. My aim is to predict 2022-05-01 18:00:00

    Please help.

    Here is code: start date: 07/01/2015 1:00, end date: 4/26/2022 15:00

    data$Category  <- as.Date(data$Category, format = "%m/%d/%Y")
    data <- data[order(data$Category), ]
    
    Y <- ts(data[,2], start = c(2015,07,01), frequency = 365*23)
    fit_arima <- auto.arima(Y, d=1, D=1, stepwise = FALSE, approximation = FALSE, trace = TRUE)
    print(summary(fit_arima))
    checkresiduals(fit_arima)
    

    Data sample:

    Category    Series ID
    07/01/2015 1:00 5244
    07/01/2015 2:00 5152
    07/01/2015 3:00 4948
    07/01/2015 4:00 4485
    07/01/2015 5:00 4231
    07/01/2015 6:00 4158
    07/01/2015 7:00 4324
    07/01/2015 8:00 4686
    07/01/2015 9:00 5060
    07/01/2015 10:00    5347
    07/01/2015 11:00    5504
    07/01/2015 12:00    4368
    07/01/2015 13:00    5440
    07/01/2015 14:00    5280
    07/01/2015 15:00    5227
    07/01/2015 16:00    5195
    07/01/2015 17:00    5195
    07/01/2015 18:00    5252
    07/01/2015 19:00    5200
    07/01/2015 20:00    5228
    07/01/2015 21:00    5244
    07/01/2015 22:00    5277
    07/01/2015 23:00    4517
    08/01/2015 0:00 4629
    08/01/2015 1:00 4996
    08/01/2015 2:00 4910
    08/01/2015 3:00 4655
    08/01/2015 4:00 4230
    08/01/2015 5:00 4000
    08/01/2015 6:00 3943
    08/01/2015 7:00 3907
    08/01/2015 8:00 4830
    08/01/2015 9:00 5298
    08/01/2015 10:00    5638
    08/01/2015 11:00    5874
    08/01/2015 12:00    3910
    08/01/2015 13:00    5864
    08/01/2015 14:00    5943
    08/01/2015 15:00    5893
    08/01/2015 16:00    5744
    08/01/2015 17:00    5573
    08/01/2015 18:00    5392
    08/01/2015 19:00    5290
    08/01/2015 20:00    5231
    08/01/2015 21:00    5152
    08/01/2015 22:00    5063
    08/01/2015 23:00    4125
    
  • Enjoy coding
    Enjoy coding about 15 years
    ya, but I want to dispose second form when button2 on form2 is clicked. how to do it?
  • nightcoder
    nightcoder almost 15 years
    It seems like if you call Close() "when it is part of a multiple-document interface (MDI) application, and the form is not visible" it is disposed anyway. At least it is in my tests.
  • Jrud
    Jrud over 14 years
    If you hook into its close event, that creates an event handler you'll have to remove as well correct? sigh memory leaks are such a pain.
  • Matthieu
    Matthieu over 12 years
    The second part of the answer is based on an old version of the framework. Anyone interested in the updated version of the Closing Event should look here : msdn.microsoft.com/en-us/library/…
  • zumalifeguard
    zumalifeguard almost 10 years
    Why would you call _Form2.Dispose() inside of the delegate, if it's already disposing?
  • M. Talha Bin Asif
    M. Talha Bin Asif almost 2 years
    I tried with 24 but forecasting is not coming out to be right.
  • alexrai93
    alexrai93 almost 2 years
    Could you please explain better what "coming out to be right" means? Maybe include the model output and what your expectation is in terms of the forecast output and seasonal periods. Note that to forecast future periods you also need to use forecast(fit, h) and specify h as the number of periods which in your case is the number of hours.