C# 使用退出按钮关闭一个 winform 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9586664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:01:10  来源:igfitidea点击:

Using Exit button to close a winform program

c#winforms

提问by Programming Newbie

I have an exit button on a winform that I want to use to close the program. I have added the button name to the FormClosed property found in the events section of the winforms properties. I thought that's all I had to do but when I click the button it does not close. I looked at the code and while a handler is created, there is no code inside of it. I don't know if that is correct or not. Here is the code that was created in the Form.cs file:

我在 winform 上有一个退出按钮,我想用它来关闭程序。我已将按钮名称添加到在 winforms 属性的 events 部分中找到的 FormClosed 属性。我以为这就是我所要做的,但是当我单击按钮时它不会关闭。我查看了代码,虽然创建了一个处理程序,但其中没有代码。我不知道这是否正确。这是在 Form.cs 文件中创建的代码:

private void btnExitProgram_Click(object sender, EventArgs e)
    {

    }

What else do I have to do?

我还需要做什么?

采纳答案by bschultz

this.Close();

Closes the form programmatically.

以编程方式关闭表单。

回答by Abbas

Put this little code in the event of the button:

把这个小代码放在按钮的事件中:

this.Close();

回答by juergen d

Try this:

尝试这个:

private void btnExitProgram_Click(object sender, EventArgs e) {
    this.Close();
}

回答by Justin Niessner

The FormClosed Event is an Event that fires when the form closes. It is not used to actually close the form. You'll need to remove anything you've added there.

FormClosed 事件是在窗体关闭时触发的事件。它不用于实际关闭表单。您需要删除在那里添加的任何内容。

All you should have to do is add the following line to your button's event handler:

您所要做的就是将以下行添加到按钮的事件处理程序中:

this.Close();

回答by Thomas Lindvall

Remove the method, I suspect you might also need to remove it from your Form.Designer.

删除该方法,我怀疑您可能还需要将其从Form.Designer.

Otherwise: Application.Exit();

除此以外: Application.Exit();

Should work.

应该管用。

That's why the designer is bad for you. :)

这就是设计师对你不利的原因。:)

回答by Brad Rogers

In Visual Studio 2015, added this to a menu for File -> Exit and in that handler put:

在 Visual Studio 2015 中,将此添加到 File -> Exit 的菜单中,并在该处理程序中放置:

this.Close();

but the IDE said 'this' was not necessary. Used the IDE suggestion with just Close();and it worked.

但是 IDE 说“这”不是必需的。使用了 IDE 建议,Close();并且它起作用了。

回答by Anayetullah

If you only want to Close the form than you can use this.Close(); else if you want the whole application to be closed use Application.Exit();

如果您只想关闭表单,则可以使用 this.Close(); 否则,如果您希望关闭整个应用程序,请使用 Application.Exit();

回答by E J Chathuranga

We can close every window using Application.Exit();Using this method we can close hidden windows also.

我们可以Application.Exit();使用这种方法关闭每个窗口,我们也可以关闭隐藏的窗口。

private void btnExitProgram_Click(object sender, EventArgs e) { Application.Exit(); }

private void btnExitProgram_Click(object sender, EventArgs e) { Application.Exit(); }

回答by Renz Macawili

You can also do like this:

你也可以这样做:

private void button2_Click(object sender, EventArgs e)
{
    System.Windows.Forms.Application.ExitThread();
}