打开一个新表单并关闭当前,而不隐藏和关闭应用程序 [英] Open a new form and close the current, without hiding and closing the application

查看:97
本文介绍了打开一个新表单并关闭当前,而不隐藏和关闭应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我最近发现了一些我以前从未真正考虑过的东西,当打开一个新表格并关闭当前时,我通常只需使用ShowDialog方法隐藏当前和显示新表单:



Hello all,

I recently came acorss something that I have never really given much thought to before, when opening a new form and closing the current, I usually just hide the current and Show the new form using the ShowDialog method:

Form2 nextForm = new Form2();<br />
this.Hide();<br />
nextForm.ShowDialog();<br />
this.Close();







但是我前几天使用ShowDialog实际获得了DialogResult想,为什么我不打开和关闭表单时只使用正常的show方法,但是如果我运行相同的代码用Show替换ShowDialog,应用程序会在form2打开后立即关闭。



使用上面的showDialog方法我猜它会产生效果,如果你有一个表单通往另一个和另一个等,所有以前的表单仍然会在内存中并具有某种性能影响。



关于使用标准Show方法的问题,我意识到它关闭应用程序的原因与Form1的close事件有关,如果它是解雇整个应用程序将关闭,我看到有些人为下一个表单创建了一个新进程,但我并不是100%理解它。



如果有人可以解释一下我会很感激,或者如果有人可以提供想要一个很好的替代解决方案。



关心高级脆皮。




However I was using the ShowDialog to actually get a DialogResult the other day and thought, why don't I just use the normal show method when opening and closing forms, but if I run the same code replacing ShowDialog with Show, the application closes as soon as form2 is opened.

Using the showDialog method above I am guessing that it will have the effect, that if you have a form leading onto another and another etc, all the previous forms will still be in memory and have some kind of performance impact.

With regards to the problem with using the standard Show method, I realise the reason it closes the application is to do with the close event of Form1, if it's fired the whole application will close, I have seen some people create a new process for the next form, but I don't 100% understand it.

If anyone could explain this I would be grateful, or if anyone could provide an alternate solution that'd be great.

Regards Senior Crispy.

推荐答案

你可以创建你自己的类派生自 ApplicationContext 。在那里,您可以拥有任意数量的表单,添加和删除表单等。当表单关闭时减少它的表单计数 - 当为0时,调用 ExitThread(); 。在program.cs中,调用 Application.Run(new YourForm()); 传递自定义上下文的实例。
You can create your own class derived from ApplicationContext. In there you can have as many forms as you like, add and remove forms etc. When forms close reduce it's form count - when 0, call ExitThread();. In program.cs, instaead of calling Application.Run(new YourForm()); pass an instnace of your custom context.


SeniorCrispy写道:
SeniorCrispy wrote:

Form2 nextForm = new Form2();

this.Hide();

nextForm.ShowDialog();

this.Close();

Form2 nextForm = new Form2();
this.Hide();
nextForm.ShowDialog();
this.Close();





代码必须像这样写虽然它没有任何区别,但我总是以这种方式编写表单隐藏的代码



the code must be written like this though it dosen't make any difference but i always write the code for form hiding in this manner anyways

Form2 obj = new Form2();
            obj.Show();
            this.Hide();





显示对话框将form2设置为模态对话框



欲了解更多数据,你可以参考这个网站

ShowDialog()



this.Close()调用此方法时,任何托管资源都可以轻松关闭,但调用dispose()方法时它将从存储器中永久删除表格



一旦你发现它有用,请评价我的答案



谢谢&问候

基数:)



Show dialog will set form2 as a modal dialog box

For more data u can refer this site
ShowDialog()

this.Close() when this method is called any managed resource can be easily closed, but when dispose() method is called it will permanently remove the form from the memory

Do rate my answer once you find it useful

Thanks & Regards
Radix :)


您似乎并不了解Show和ShowDialog方法之间的真正区别或程序实际运行的方式。



当您的C#程序启动时,应用程序所做的第一件事就是创建一个新表单,无论哪种表单是您的主表单然后运行它。该表单关闭时,应用程序将退出。来自MSDN:此方法为Closed事件的mainForm参数添加事件处理程序。事件处理程序调用ExitThread来清理应用程序。



这意味着每当你的主表单关闭时,应用程序将关闭,除非你的Program.cs在那次调用之后有任何内容。



对于Show和ShowDialog之间的区别,ShowDialog做了同样基本的东西。当该行代码运行时,它将等待该表单关闭以运行下一行代码。所以使用你的代码,

It doesn't appear that you understand what the real difference between the Show and ShowDialog methods or how the program actually runs.

When your C# program starts, the first thing the application does is to create a new form of whichever form is your main form and then run it. The application will quit when that form is closed. From MSDN: "This method adds an event handler to the mainForm parameter for the Closed event. The event handler calls ExitThread to clean up the application."

This means that whenever your main form is closed, the application will close unless your Program.cs has anything after that call.

As to the difference between Show and ShowDialog, ShowDialog does the same basic thing. When that line of code runs, it will wait until that form has closed to run the next line of code. So with your code,
Form2 nextForm = new Form2();
this.Hide();
nextForm.ShowDialog();
this.Close(); 



this.Close(); nextForm 已关闭。






this.Close(); will not be hit until nextForm is closed.


SeniorCrispy写道:
SeniorCrispy wrote:

所有以前的表单仍然会在内存中并对性能产生某种影响。

all the previous forms will still be in memory and have some kind of performance impact.





这是真的。如果您不关闭表单,它仍将在内存中。但是,您必须保持主窗体打开或应用程序关闭。但是,老实说,保持主要形式的开放实际上对记忆的使用非常微不足道。



That's true. If you don't close a form, it will still be in memory. However, you have to keep your main form open or the application will close. But, honestly, keeping the main form open is really a pretty insignificant use of memory.


这篇关于打开一个新表单并关闭当前,而不隐藏和关闭应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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