开发者

Correct calling of Dispose() for opened form

Let's suppose I have 2 forms: FormA and FormB. On FormA button click the instance of FormB is created and shown.

How to Dispose() FormB instance correctly after it has been closed?

To be more precise, let's suppose this is the code that creates a form:

  开发者_开发百科  public void Settings(object sender, EventArgs e)
    {
        if (_settings == null)
            _settings = new Settings(_repositoryCollection, _config, this);

        _settings.Show();
        _settings.Focus();
    }


If you want a modal dialog, use

using (var settings = new Settings(_repositoryCollection, _config, this))
{
   settings.ShowDialog ();
}

Otherwise, for a normal form shown at the same time as FormA... you may not even have to. See this post.:

_settings = new Settings(_repositoryCollection, _config, this);
_settings.Closed += delegate {_settings.Dispose ();};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜