3

I have searched through stackoverflow and has found similar question.

As I am still a beginner in C# programming, I could not quite understand what the solutions meant.

I have a c# windows form application that has several forms.

As it only closes when I closes the main form, how do I code it such that when I close other form the application closes as well?

EDIT: most people has told me to code it in the closing event. may i know where is the event found at? thanks.

Otiel
  • 18,404
  • 16
  • 78
  • 126
Thomas
  • 1,126
  • 6
  • 21
  • 39

4 Answers4

2

Not sure why you think out answers will be any easier than ones you have found already but..

A Form has an event called "Closing" (See Here). You should handle the event and then inside that function you can use the following line to close your application...

System.Windows.Forms.Application.Exit();

NOTE: This is for a WinForms application

EDIT:

You can register the closing event using VS designer mode like this...

  1. Select the form
  2. Go To the "Events" window
  3. Double click the "Closing" event option

More detailed answer here

(Google if you cannot work this out)

or you can register the event in code like this...

//In Form constructor function
this.Closing += new EventHandler(Form1_Closing);

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
   //Handle event here
   System.Windows.Forms.Application.Exit();
}
Community
  • 1
  • 1
musefan
  • 47,875
  • 21
  • 135
  • 185
  • hi thanks for the answer. where can I find the event "closing"? – Thomas Oct 31 '11 at 14:08
  • oh my. I am so grateful for your answer. it works!! thanks a million! – Thomas Oct 31 '11 at 14:17
  • @Thomas: Glad to help, remember to upvote and accept any answers that help you out (including past questions) it will help you get better responses next time you need to ask a question – musefan Oct 31 '11 at 14:25
  • okay. will do. but just that this is my new account here and I do not have enough reputation to upvote yet. will do so when I have enough reputation! – Thomas Oct 31 '11 at 14:26
1

In WPF you can call Application.Current.Shutdown to close the application. Register in your window to the Window.Closed-Event and call Application.Current.Shutdown()

Update For Winforms, use the FormClosedEvent or override OnFormClosed. As alreads written by others, use Application.Exit to terminate the app.
WPF stands for Windows Presentation Foundation and is a framework for building nice user interfaces

HCL
  • 36,053
  • 27
  • 163
  • 213
  • not sure what is pdf but I am currently doing it on windows form. may I know where can I find the event? – Thomas Oct 31 '11 at 14:08
1

Its very simple. Use the following code if you want to exit from the application from any point of time.

Application.Exit();
Nps
  • 1,638
  • 4
  • 20
  • 40
0

Check out Application.Exit Method

Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.

There is also an overload that allows forms receiving the quit event to inform it wants to cancel.

Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121