Always Get Better

Never stop looking for ways to improve

April 4th, 2008

When working with a Windows GUI, it may seem unclear whether to use Form.Close() or Form.Dispose() to get rid of a dialog at runtime.

Form.Close() removes the dialog from sight and calls the Closing() and Closed() methods.  You can still access the form and bring it back later on.

Form.Dispose() destroys the dialog and frees its resources back to the operating system.   It does not call the form’s Closing() and Closed() methods. Once disposed, you may not recall a form.

Which to use? If you have no logic in the form’s close methods and don’t intend to re-use the form, go with Dispose().  Otherwise, go with Close().  Some programmers aren’t sure which to use, and they use both – Close() then Dispose()!

  • Mike Wilson (165)
  • 7 Responses to “ C#: Form.Close() vs Form.Dispose() ”

    1. NPT says:

      “Form.Dispose() destroys the dialog and frees its resources back to the operating system. It does not call the form’s Closing() and Closed() methods. Once disposed, you may not recall a form.”

      It certainly does Close the form. When disposing a form, it releases all resources that its using, including the resources keeping the form on the screen. Here’s an example…

      using(Form f = new Form())
      {
      f.Show();

      // Do some stuff
      }

      This will close AND dispose the form without ever calling Close.

    2. mwilson says:

      Yes – Form.Dispose() does close the form. But it doesn’t call the Closing and Closed methods. If you have any code logic triggered by the form being closed, it will NOT be executed when the form is Disposed, unless you make a call to Close() first. Otherwise the form is just silently taken away.

    3. James says:

      In this case it makes sense to call both. Or call .Close() within a using construct. It allows the most graceful shutdown for a form, frees up resources right away, and suppresses the finalizer.

    4. lellis says:

      “Which to use? If you have no logic in the form’s close methods and don’t intend to re-use the form, go with Dispose(). Otherwise, go with Close(). Some programmers aren’t sure which to use, and they use both – Close() then Dispose()”

      Actually, sometimes it is better to call dispose IF you have logic in the close event! For example, I have a form with logic in the closing event.
      The logic just cancels the close. At some point, I *really* want to close the form, and the easiest way to do it is to call the dispose method. This
      way I don’t need special logic in the closing event to deal with the special case.

    5. xpin says:

      I read MSDN and find this text:
      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.

      But it is false.
      1)I create form without showing.
      2)I delegate more methods of this form for more classes.
      3)I make FormClosing event in designcode and write e.cancel = true;
      4)I make FormClosed event in designcode and exclude all delegate metods.
      5)After create form, I write form.Close(); (Certain, since there is event of the cancelling).
      6)I make show on click button! – Has exception, because form is disposed!

      This is result of “Handle==0″. When property Handle don’t read and not created,
      my events FormClosed and FormClosing is not call. And I have more problems and bag.

      I write code after create of form:
      if (form.Handle != (IntPtr)0) form.Tag = 0;
      - has 0 bag and 0 problem.

      Since this bug .NET or lying MSDN

      best regards,
      xpin

    6. Quadko says:

      if I understand that last problem posted by xpin, I think the issue isn’t MSDN or a bug, exactly, but that the form was never shown in the first place.

      That Handle==0 issue is a common bug resulting from delayed acquiring of a window handle from the OS. If you don’t show the Form, the handle is never created – same state as after Dispose when it is released. That code to reference form.Handle is the common workaround to force the handle to be created. This isn’t really related to Close() verses Dispose(), just is related to Forms.

      And btw, form.Close() balances form.Show(), the way file.Close() balances file.Open().

    7. Form.Close() removes the dialog from sight and calls the Closing() and Closed() methods. You can still access the form and bring it back later on. says:

      Form.Close() removes the dialog from sight and calls the Closing() and Closed() methods. You can still access the form and bring it back later on.

    Leave a Reply