Using the Exit Button to close winform

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

private void btnExitProgram_Click(object sender, EventArgs e) { } 

What else do I need to do?

+15
c # winforms
source share
9 answers
 this.Close(); 

Closes the form programmatically.

+36
source share

Delete this method, I suspect you might also need to remove it from Form.Designer .

Otherwise: Application.Exit();

Must work.

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

+12
source share

FormClosed event - an event that fires when the form closes. It is not used to actually close the form. You will need to delete everything that you added there.

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

 this.Close(); 
+4
source share

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

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

+2
source share

Put this little code in the button case:

 this.Close(); 
+1
source share

Try the following:

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

In Visual Studio 2015, this is added in the menu File โ†’ Exit and in this handler:

 this.Close(); 

but the IDE said that 'this' is not required. Used the IDE clause only with Close(); and it worked.

0
source share

If you only want to close the form, you can use this.Close (); otherwise, if you want the entire application to be closed, use Application.Exit ();

0
source share

You can also do it like this:

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

All Articles