C# 如何使Winforms窗体终止应用程序?
我正在用C#编写Winforms。我想设置我的程序,以便在窗体关闭时,程序将终止或停止。我已经知道如何选择将首先打开的表单 这是my program.cs中的代码:C# 如何使Winforms窗体终止应用程序?,c#,.net,winforms,terminate,C#,.net,Winforms,Terminate,我正在用C#编写Winforms。我想设置我的程序,以便在窗体关闭时,程序将终止或停止。我已经知道如何选择将首先打开的表单 这是my program.cs中的代码: using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace MainMenu { static class Program { /// <sum
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace MainMenu
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Login());
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Windows.Forms;
名称空间主菜单
{
静态类程序
{
///
///应用程序的主要入口点。
///
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
运行(新登录());
}
}
}
我想设置login.cs表单,以便在该表单关闭时,程序将终止。这可能吗
`要在表单关闭时执行某些操作,您需要。在事件处理程序中,调用
Application.Exit()
终止应用程序
使用上面链接的答案中的模型(但在C中),使用此事件处理程序:
private void Form_Closing(Object sender, FormClosingEventArgs e)
{
Application.Exit();
}
要添加处理程序,只需在表单类中将该方法注册到事件中(基于MSDN的讨论):
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(Form_Closing);