我想在主表单关闭时离开应用程序



我有一个登录表单,在接受主表单登录后运行,并且这种登录形式是隐藏的,但是当我关闭主表单时,程序仍在运行。我使用了这个.close(),但是在登录主窗体关闭后!

我想在主窗体关闭时离开应用程序

 UserBLL x = new UserBLL();
            if (x.loginAcount(TextBox1.Text, TextBox2.Text) == true)
            {
                MessageBox.Show("You are successfully logged in", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Hide();
                MainForm frm = new MainForm();
                frm.ShowDialog();

            }
            else
            {
                MessageBox.Show("Username or password is incorrect", "Info", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

如果要退出应用程序,请添加此行

 Application.Exit();

要检测主窗体是否已关闭,您需要使用 FormClosingEventHandler 并在检测到主窗体已关闭后退出应用程序

  this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.yourMainForm_FormClosing);

private void yourMainForm_FormClosing(object sender, FormClosingEventArgs e)
{
  //here you can exit from application 
  Application.Exit();
}

相关内容

最新更新