+1  A: 
  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.

Hope this helps, if you've any more questions, just ask them in the comments.

Bye

BW

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.

Is this sufficient for your purposes? If not, whats missing?

Binary Worrier
ya, but I want to dispose second form when button2 on form2 is clicked. how to do it?
Enjoy coding
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.
Jrud
+4  A: 

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.

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.

Igor Brejc
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.
nightcoder
+1  A: 

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.

PoweRoy
+2  A: 

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();
    }
}
Lasse V. Karlsen