首页> C#>如何关闭主窗体而不让应用程序关闭? [英] c# - How to close the main form without letting the application close?
问题描述
我想做什么:
->打开MDI表单
->关闭项目主窗体
->不关闭应用程序
What i want to do:
->Open a MDI form
->Close the projects main form
->Without closing the application
我做了什么:
frmMain fm = new frmMain();
fm.Show();
this.Close()
任何帮助将不胜感激!:)
推荐答案
如果您确实希望关闭主窗体,但保持应用程序运行,则可以使默认类启动应用程序上下文:
If you actually wish to close the main form, but leave the application running, you can make your default class launch the application context:
using(MyContext myContext = new MyContext())
{
Applicaton.Run(myContext);
}
其中 MyContext
继承自 ApplicatonContext
.
然后,您的应用程序上下文可以根据需要加载和关闭表单.我使用一个项目执行此操作,在该项目中,上下文启动了登录表单.仅在成功完成之后,才会加载主表单.我还允许终止主表单并重新加载登录表单,以处理程序中的注销".
Your application context can then load and close forms as needed. I do this with one project where I have the context launch a login form. Only after it is completed successfully does the main form get loaded. I also allow the "logging out" from the program to be handled by terminating the main form and reloading the login form.
从注释中,可能您正在寻找的只是.Hide().这不会导致您的表单被卸载,并且对.Show()的调用将恢复该表单(例如,您可以使用计时器或其他包含对您的主表单的引用的表单来进行调用).我更喜欢使用ApplicationContext,因为它负责使程序在内存中的位置远离用户可能试图关闭的UI元素.
From the comments, it may be that all you are looking for is .Hide(). This doesn't cause your form to unload and a call to .Show() will restore it (for example, you can use a timer or another form holding a reference to your main form to make the call). I prefer to use an ApplicationContext because it takes the responsibility for keeping your program in memory away from a UI element which the user may attempt to close.
这篇关于首页> C#>如何关闭主窗体而不让应用程序关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!