C# Application.Exit()工作不正常

C# Application.Exit()工作不正常,c#,winforms,exit,C#,Winforms,Exit,我有一个单独的windows窗体供用户选择背景音乐,除非应用程序关闭,否则它将始终保留在那里。我使用以下代码阻止用户关闭音乐窗体: private void Music_FormClosing(object sender, FormClosingEventArgs e) { e.Cancel = true; } 在main Program.cs中,我运行了一个名为login的页面,如下所示: Application.Run(new Login()); 在我的所

我有一个单独的windows窗体供用户选择背景音乐,除非应用程序关闭,否则它将始终保留在那里。我使用以下代码阻止用户关闭音乐窗体:

private void Music_FormClosing(object sender, FormClosingEventArgs e)
    {
        e.Cancel = true;
    }
在main Program.cs中,我运行了一个名为login的页面,如下所示:

Application.Run(new Login());
在我的所有表单中,我还有一个FormClosed事件,在按下十字后关闭整个程序

private void Login_FormClosed(object sender, FormClosedEventArgs e)
    {
        Application.Exit();
    }
但是,在我添加了用户无法关闭音乐表单的代码后,我的应用程序只能在登录页面中按叉键退出,这是主页运行的程序的开始(应用程序过去可以通过按叉键退出我的所有表单)


我想知道是否有任何方法可以让我正确退出我的应用程序,或者让用户无法关闭音乐表单而不影响其他表单的关闭。谢谢

音乐表单关闭中的代码阻止您的应用程序退出。要获得所需的行为(防止用户关闭音乐窗体),您可以使用
FormClosingEventArgs
属性:

private void Music_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing)
        e.Cancel = true;
}

Music\u FormClosing
中的代码阻止您的应用程序退出。要获得所需的行为(防止用户关闭音乐窗体),您可以使用
FormClosingEventArgs
属性:

private void Music_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing)
        e.Cancel = true;
}

您无法使用音乐窗体关闭,因为您正在使用
e.Cancel=true取消该操作。。是吗?是的,我的意思是让用户无法关闭音乐窗体,只能通过关闭主窗体来关闭,然后只需替换
Application.Exit()
环境。退出(0)。因为
Application.Exit()
正在引发
Music\u FormClosing
,这使其失败。您无法使用音乐表单关闭,因为您正在使用
e.Cancel=true取消该操作。。是吗?是的,我的意思是让用户无法关闭音乐窗体,只能通过关闭主窗体来关闭,然后只需替换
Application.Exit()
环境。退出(0)。因为
Application.Exit()
正在引发
Music\u FormClosing
,这使其失败。出现一个错误,称“CloseReason”是一个类型,在给定的上下文中无效,只需输入错误即可(因此编辑器键入)。正如我在回答中提到的,它是传递的
FormClosingEventArgs
的一个属性。有一个错误说“CloseReason”是一个类型,它在给定的上下文中无效(因此编辑器键入)。正如我在回答中提到的,它是传递的
FormClosingEventArgs
的一个属性。