Wine Too
Wine Too

Reputation: 4655

Where to dispose instance of non modal form

It is clear with modal forms...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim f As New myForm
    f.ShowDialog(Me)
    f.Dispose
End Sub

But what to do with non modal forms?
Where to dispose it?

    Dim f As New myForm
    f.Show(Me)
    f.Dispose

This will close newly created form immediately so f.Dispose shouldn't stay here.
If I put that in _FormClosing handler will be nice but not enough since we can have few instances of that form running.

1) So, where and how to dispose non modal forms opened like in second example?
2) Is here any event to know that our child form closes?

Upvotes: 2

Views: 858

Answers (1)

Karl Anderson
Karl Anderson

Reputation: 34846

You do not need to call Dispose on a modeless form, because the resources of the form are cleaned up for automatically, except for the following two conditions:

  1. it is part of a multiple-document interface (MDI) application, and the form is not visible
  2. you have displayed the form using ShowDialog

Your scenario does not fall into either of the above two conditions, so you do not need to worry about where to put a manual .Dispose() call, because it is not needed.

Read Form.Close Method documentation for more information.

Upvotes: 3

Related Questions