下面的代码成功地调用了另一个表单,显示了它,隐藏了第一个表单,并对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);不起作用.

推荐答案

您必须在打开的每个表单上绑定一个事件.确保您不会try 将此事件绑定到类型Form2,您不能在非对象实例上绑定事件.您需要将其绑定到您使用= new Form2()创建的实例form2

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.Closed += (sender, e) => this.Close(); //note that you bind it to the instance of `Form2` rather than the Type
            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();
        }
    }
}

Csharp相关问答推荐

List T.AddRange在传递ConcurrentDictionary作为参数时引发ArgumentExcellent

如何打印已添加到List的Linq值,而不是C#中的:System.Collections.Generic.List ' 1[System.Int32]?

ß != ss与ICU进行不区分大小写的比较

在Linq中调用需要limit和offset的方法''''

在C#WinUI中,一个关于System的崩溃."由于未知原因导致执行不例外"

(乌龙)1&#比c#中的UL&#慢吗?

在命名管道上使用GRPC ASP.NET核心时如何配置命名管道权限

使用带有参数和曲面的注入失败(&Q;)

JsonSchema.Net删除假阳性判断结果

在PostgreSQL上使用ExecuteSqlRawAsync的C#11原始字符串文字有区分大小写的问题

为什么ReadOnlySpan;T&>没有Slice(...)的重载接受Range实例的?

此异步方法在重写方法中缺少等待运算符警告

解决方案:延长ABP框架和ANGING OpenIddict中的令牌生命周期

Visual Studio,Docker容器-容器调用:连接被拒绝

如何在C#中从MongoDB IPipelineStageDefinition中获取聚合命令的字段/选项?

游戏对象走向不同的方向

无法通过服务控制台启动.NET Core 6.0服务(错误1053)

使用c#中的Windows 10关机消息

如何模拟一个返回OneOf IServiceA,IServiceB的方法?使用Moq

Roslyn编译器看不到引用