下面的代码成功地调用了另一个表单,显示了它,隐藏了第一个表单,并对Form2重复了相同的过程.(共有3个表格). 但如果有人在应用程序中间使用X关闭应用程序.(在Form2或ForM3中),应用程序一直在后台运行,我必须使用任务管理器手动终止它.
我是一名学生,所以我的代码可能是相当未优化的,但尽管在互联网上寻找答案,我还是找不到解决方案.
using System;
using System.Windows.Forms;
using System.Diagnostics;
namespace FormSurveyOpdracht
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnNext_Click(object sender, EventArgs e) //When Next button is clicked:
{
//Rest of the Code
Form2 form2 = new Form2(fullname); //Define a new form Form2, and send the variable fullname.
form2.Show(); //Show the Form2.
this.Hide(); //Hide this Form (Form1)
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e) //if the form is closed in an unexpected way, exit the whole application.
{
Application.Exit();
}
}
}
我try 了什么:我try 使用X关闭应用程序. 我期待的是:整个应用程序将关闭. 事件:应用程序一直在后台运行.
我还试了什么:我试着把一个
private void Form1_FormClosing(object sender, FormClosingEventArgs e) //if the form is closed in an unexpected way, exit the whole application.
{
Application.Exit();
}
在每个表单上(Form1代表Form1,Form2代表Form2,等等) 结果:应用程序一直在运行. 我还try 了:Application.ExitThread();和Environment(0);不起作用.