我正在用 C# 编写 Winforms。我想设置我的程序,以便在关闭表单时程序将终止或停止。我已经知道如何选择将首先打开的表单。
这是我的program.cs中的代码:
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());
}
}
}
我想设置我的 login.cs 表单,这样如果该表单关闭,程序就会终止。这可能吗?
`
最佳答案
要在关闭表单时执行某些操作,您需要 handle the FormClosing event .在您的事件处理程序中,调用 Application.Exit()
以终止应用程序。
使用上面链接的答案中的模型(但在 C# 中),使用此事件处理程序:
private void Form_Closing(Object sender, FormClosingEventArgs e)
{
Application.Exit();
}
要添加处理程序,只需将方法注册到您的表单类中的事件(基于来自 MSDN 的 this 讨论):
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(Form_Closing);
关于c# - 如何使 Winforms 表单终止应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46638255/