从一个切换到另一个时如何关闭表格. [英] How to close forms,when switching from one to another.

查看:74
本文介绍了从一个切换到另一个时如何关闭表格.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我需要一个建议,我有一个智能设备应用程序,并且我必须从一种形式转到另一种形式.例如,当我打开Form2时,我有两种形式Form1和Form2,Form1应该关闭.我使用此代码:

Hello all,
I need an advice,i have a smartdevice application,and i have to go from one form to another.For example,i have two forms Form1 and Form2,when i open Form2,Form1 should close. I use this code:

Form2 myNewForm = new Form2();
            
            myNewForm.Visible = true;

            myNewForm.ShowDialog();
        
            this.Close();


但是当我关闭应用程序时,使用Application.Exit()可以看到窗体的级联关闭.

任何帮助该怎么做?
预先感谢


but when i close the application,with Application.Exit() i can see the cascade of Forms closing.

Any Help how to do this?
Thanx in advance

推荐答案

Form2 myNewForm = new Form2();
            
            myNewForm.Visible = true;
 
            myNewForm.ShowDialog();
        
            form1.hide();


从问题中给出的代码看来,myNewForm 是在current form中声明的.因此,即使this.Close();被称为GC也不会释放由此形式引用的当前形式保存的内存,因为对此形式保留了对myNewForm 的引用.
因此,在可从此表单访问的其他位置声明myNewForm .
myNewForm.Visible = true;是多余的,因为myNewForm.ShowDialog();使其可见.
最好从表单外部调用Dispose 方法,例如Fom1.Dispose() 以便GC立即处置.
From the code given in the question it appears that the myNewForm is declared in the current form. Hence, eventhough this.Close(); is called the GC will not release the memory held by the present form referred by this, as a reference to myNewForm is held by this form.
So, declare myNewForm in another location accessible from this form.
myNewForm.Visible = true; is redundant as myNewForm.ShowDialog(); makes it visible.
It is better to call Dispose method from outside the form, like Fom1.Dispose() for immediate disposal by GC.


这篇关于从一个切换到另一个时如何关闭表格.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆