开发者

Correct calling of Dispose() for opened form

开发者 https://www.devze.com 2023-03-19 19:24 出处:网络
Let\'s suppose I have 2 forms: FormA and FormB. On FormA button click the instance of FormB is created and shown.

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

精彩评论

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