winForm 子窗体间相互切换时,关闭当前子窗体并释放资源

首先在父窗体中判断该窗体是否已经存在

        public static frmBookSelect fbs;
        private void tsbBookSelect_Click(object sender, EventArgs e)
        {
            if (fbs == null)          //如果不存在就实例化该窗体并显示
            {
                fbs = new frmBookSelect();
                fbs.MdiParent = this;

                //fba.MdiParent = this;

                //fu.MdiParent = this;

                fbs.Show();
            }
            else                       //存在就直接把该窗体激活
            {
                fbs.Activate();
            }

在关闭子窗体时释放资源,并给窗体赋值为null

   private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();      //.NET中本身就带有GC机制,在关闭窗体的时候已经释放资源了
            frmMain.fbs = null;       
        }


你可能感兴趣的:(技术类)