简体   繁体   中英

Using Exit button to close a winform program

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:

private void btnExitProgram_Click(object sender, EventArgs e)
    {

    }

What else do I have to do?

this.Close();

以编程方式关闭表单。

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

Otherwise: Application.Exit();

Should work.

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

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.

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

this.Close();

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

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

使用以下代码

System.Windows.Forms.Application.Exit( ) 

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

this.Close();

Try this:

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

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

this.Close();

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

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();

You can also do like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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