Winforms OnForm的关闭和处理/关闭

问题描述 投票:0回答:1

我有一个要覆盖方法OnFormClosing的表单。此方法是否需要显式调用Close或Dispose?

这是我的父母表格

public partial class MyParentForm : Form
{

   // Other methods

   public void RunChildForm()
   {
      Hide();

      var child = new MyChildForm()
      {
         Owner = this
      };

      child.Play();
   }

}

这是我的孩子表格

public partial class MyChildForm : Form
{

   // Other methods

   public void Play()
   {
      Show();
   }

   protected override void OnFormClosing(FormClosingEventArgs e)
   {

      // WHAT IS THE CORRECT ORDER?
      // Some code
      ...

      // Display the owner form
      Owner.Show(); 

      base.OnFormClosing(e);

      // Here, is the Form Disposed/Closed? I am unsure if this is necessary.
      Close();
      Dispose();
   }
}

执行顺序应该是什么?我要清理子窗体,然后再次显示父窗体。

c# winforms dispose
1个回答
0
投票

您可能真的不想“重写OnFormClosing方法”。

根据您的发言,最好的选择是为“ FormClosing”编写一个事件处理程序。

© www.soinside.com 2019 - 2024. All rights reserved.