简体   繁体   English

button_click事件后如何关闭Winform窗口

[英]How to close winform window after button_click event

I have program in Winfroms. 我在Winfroms中有程序。 I use a lot of check boxes to pick my options. 我使用很多复选框来选择我的选项。 And there is main button1 to click after options choosing. 在选择选项后,有一个主按钮1单击。 Everything works, I click on it, everything is generated right, but my winforms window still stays and doesn't go away. 一切正常,我单击它,一切都正确生成,但是我的winforms窗口仍然存在并且不会消失。 I need to click on exit mark. 我需要点击退出标记。 Is it possible to run and exit program after button1 click? 单击button1后是否可以运行和退出程序?

My code of button click event: 我的按钮点击事件代码:

private void button1_Click(object sender, EventArgs e)
{
    var header = File.ReadAllText(@"dir/header");
    var footer = File.ReadAllText(@"dir/footer");
    var sb = new StringBuilder();
    sb.AppendLine(header);
    if (checkBox1.CheckState == CheckState.Checked) sb.AppendLine(@"\include{chap2}");
    if (checkBox2.CheckState == CheckState.Checked) sb.AppendLine(@"\include{chap3}");
    sb.AppendLine(footer);
    File.WriteAllText(@"dir/final", sb.ToString());

I found tutorials about this problem, but they all have the same problem. 我找到了有关此问题的教程,但是它们都有相同的问题。 You need to click the exit mark and only then you can get out of winform window. 您需要单击退出标记,然后才能退出winform窗口。

  1. If the button is in the main form you can simply use Close() to exit the program 如果按钮位于主窗体中,则可以简单地使用Close()退出程序

     this.Close(); 
  2. If you're going to exit the program from another form, you have to use Application.Exit() 如果要从其他表单退出程序,则必须使用Application.Exit()

  3. There's another way of closing the application called Environment.Exit ; 关闭应用程序的另一种方法称为Environment.Exit something like a force close which as MSDN says: 如MSDN所说的强制关闭:

Terminates this process and returns an exit code to the operating system. 终止此过程并将退出代码返回到操作系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM