4

So my issue is pretty straight-forward, I have a program with a login feature, and if all goes well it will open up another form and hide itself.

My issue is that in the new form that opens, hitting the X button to close the window will only close the form but leave the other one running, along with the process.

I have a quit button that shuts down the program, but is there any way to make the X button shut down the program entirely?

Thanks!

Max Box
  • 41
  • 1
  • 10
  • Possible duplicate of [How to properly exit a C# application?](http://stackoverflow.com/questions/12977924/how-to-properly-exit-a-c-sharp-application) – Jannik Mar 08 '16 at 06:12

4 Answers4

3

You need to handle the Closed or 'FormClosed' event. This event is raised when form is closed.

use either of these when creating childform.

childForm.Closed += (s, ev) => Application.Exit();
childForm.FormClosed += (s, ev) => Application.Exit();

Update :

Sorry, I might have over looked the last part in question, since you have X button which has the Click event, you can simply call Application.Exit(); on that event.

Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
  • No no, I mean the close button that all windows have. Something like this? - https://i.gyazo.com/31ef37a79e36e0a47db7029980714919.png - That still throws up an error. – Max Box Mar 08 '16 at 06:12
  • In that case, place _either_ of the even subscription logic after `childform` instance creation. It should work. – Hari Prasad Mar 08 '16 at 06:15
  • 1
    this.Closed / this.FormClosed – Jannik Mar 08 '16 at 06:16
  • 1
    Looking at the screenshot, I notice you are doing it in the `MainForm` constructor, in this case use `this.Closed += (s, ev) => Application.Exit();` – Hari Prasad Mar 08 '16 at 06:17
  • Oh man, it took a while but we got there in the end. Thank you so much for your help! – Max Box Mar 08 '16 at 06:21
0

Have you tried using Application.Exit() in your close event handler.Also if you have created any threads you need to make sure that you convert them to background.

Rohit
  • 10,056
  • 7
  • 50
  • 82
  • Thanks for replying! Still pretty new to C# and such, how would I do this? – Max Box Mar 08 '16 at 04:48
  • @MaxBox please take a look at my updated answer..It has a link in it – Rohit Mar 08 '16 at 04:50
  • In your close button code just add Application.Exit() – Rohit Mar 08 '16 at 04:56
  • Where can I find the close button code? I have my exit button working perfectly, but I mean the top right X on all windows. Also, if I need to make it myself, how can I do that? Thanks! – Max Box Mar 08 '16 at 05:20
  • private void MainForm_FormClosing(Object sender, FormClosingEventArgs e) { Application.Exit(); } Something like that? – Max Box Mar 08 '16 at 05:39
  • https://i.gyazo.com/c45c14a626b13c2dda79c2e1cce15160.png - Like this? Because if so, that doesn't work, hitting close still keeps the process open in the background. – Max Box Mar 08 '16 at 06:16
0

Use Application.Exit()in your button click event handler. According to the Documentation, it terminates all message loops and closes all windows thus giving your forms the possibility to execute their cleanup code (in Form.OnClose etc).

VVN
  • 1,607
  • 2
  • 16
  • 25
  • Hey there! So really, I'm still super new to this and have no idea how I would go about doing this. I've been working with C++ Console applications for a while, I have no idea how I would. – Max Box Mar 08 '16 at 04:52
  • Put this code in your **'X' button** click event handler. – VVN Mar 08 '16 at 04:54
  • Where can I find the X button click event handler? And if I need to add it myself, how can I do that? Thanks! – Max Box Mar 08 '16 at 05:20
  • private void MainForm_FormClosing(Object sender, FormClosingEventArgs e) { Application.Exit(); } Something like that? – Max Box Mar 08 '16 at 05:40
  • Yes.Or you can override the behaviour for some another operations. – VVN Mar 08 '16 at 05:41
  • https://i.gyazo.com/f10397f1357dd822d8a56117a4c19293.png https://i.gyazo.com/6647659e10ca1fd14ecbfd5a4c7a0a90.png That's what I currently have, if I then hit close, it remains open in the background. – Max Box Mar 08 '16 at 05:46
  • Then use a event handler like this: `childForm.Closed += (s, cf) => Application.Exit();` – VVN Mar 08 '16 at 05:48
  • https://i.gyazo.com/1d7b4ee8440f1ba3ba9d20be75ec78e6.png - this throws up some errors, what am I doing wrong? – Max Box Mar 08 '16 at 05:54
  • @MaxBox put that in the constructor, right after the line `InitializeComponent()` – Jcl Mar 08 '16 at 05:59
  • https://i.gyazo.com/0c64d62f34fc896a62a26a040c8a6f12.png - This still gives an error, and if I don't rename it I get this - https://i.gyazo.com/66c3cbdc04c24f47668a18c7f953a066.png – Max Box Mar 08 '16 at 06:05
  • Make it as `this..Closed += (s, cf) => Application.Exit();` – VVN Mar 08 '16 at 06:28
0

If u have Form1, Form2 and need close Form2 in Close button write

this.Close();

If need close Form1(Main) then

Application.Exit();