我想要做什么:
->打开一个MDI表单
->关闭项目主窗体
->不关闭应用程序
我做了什么:
frmMain fm = new frmMain();
fm.Show();
this.Close()
任何帮助,将不胜感激!:)
您可以使用隐藏方法。
尝试这样做:
frmMain fm = new frmMain();
this.Hide();
fm.Show();
或者:
frmMain fm = new frmMain();
this.Hide();
fm.ShowDialog();
this.Show();
后者将打开窗口,一旦关闭,前一个窗口将重新出现。
我为解决这个问题所做的是:
this.Hide()
可能不是最好的方法,但仍然如此。
你甚至可以通过调用来取回它
this.Show()
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);
}
where MyContext
inherits from 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.
EDIT: 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.