c# - 如何使 Winforms 表单终止应用程序?

标签 c# .net winforms terminate

我正在用 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/

相关文章:

.net - Visual Studio 模式(常规、Web 开发、C#)之间的差异?

c# - RichTextBox 选择同一单词的多次出现

c# - .net Core 2.0 web api 400错误使用Validateantiforgerytoken

javascript - 在文本框中按下 Enter 键时,重定向到 page.aspx

c# - web.config 连接字符串在发布时更改

c# - 我应该为创建和更新使用不同的DTO吗? (CRUD)

c# - 有没有办法使 row.DefaultCellStyle.BackColor 固定,尽管重新绘制?

c# - faSTLine 图表中显示的空数据点不正确

c# - 查看不断变化的数据集的聚合的最佳架构选择是什么?

c# - 带有 SSH.NET 的 Linux : “sorry, you must have a tty to run sudo”