How do I extend a WinForm's Dispose method?

谁说我不能喝 提交于 2019-11-27 12:58:50
heavyd

You need to override the Dispose method from Form

Typically this is automatically overridden in the RestartForm.Designer.cs file, so you will need to move the dispose into your code file so that you can add whatever code you need to add without it being rewritten by the designer.

In the RestartForm.cs

protected override void Dispose(bool disposing)
{
  if (disposing)
  {
    if (components != null)
    {
      components.Dispose();
    }

    // Dispose stuff here
  }

  base.Dispose(disposing);
}

I use this method :)

            Image bgImage = Image.FromFile(workingDir + "\\" + button.BackgroundImage);
            currentButton.Image = bgImage;
            currentButton.Disposed += (Object sndr, EventArgs evnt) => bgImage.Dispose();

If RestartForm extends System.Windows.Forms.Form, you should be able to override Dispose(bool disposing). You should properly implement this for your "RestartForm" class to dispose of your IDisposables.

It should look like:

public override Dispose(bool disposing)
{
   if (disposing)
   {
       // Dispose was called from user code. Dispose of managed resources here.
       done.Dispose();
   }

   // Dispose of unmanaged resources here, and invoke base dispose.
   base.Dispose(disposing);
}

You need to override the Dispose method, this method comes from the Control base class

protected override void Dispose(bool disposing)
{
  if (disposing)
  {
    event.Dispose();
  }
  base.Dispose(disposing);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!