WinForm中防止关闭启动窗体后应用程序退出的解决方案
时间:2011-06-05 来源:SweetBoy
1、首先新建一个类MyAppContext ,继承自System.Windows.Forms.ApplicationContext类
public class MyApplicationContext: ApplicationContext
{
public MyApplicationContext()
{
LoginForm startForm = new LoginForm();
startForm.Show();
}
}
2、然后在Program.cs文件中修改成如下代码
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyApplicationContext());
}
}
3、然后在项目里面新建一个名为BaseForm的基窗体
public partial class BaseForm : Form
{
public BaseForm()
{
InitializeComponent();
this.FormClosed += (sender, e) => {
if (Application.OpenForms.Count == 0)
{
Application.Exit();
}
};
}
}
4、然后新建其它窗体,并继承自BaseForm.
相关阅读 更多 +
排行榜 更多 +