I have two Forms in my project. The first one I made makes the program exit if I hit 'X' button at the top of the window which is obviously normal.

But that's not the case with the second form where If I hit the 'X' button then the Form closes but the actual program still runs.

How do I make it so that it closes the program when I hit the X button?

I don't understand...Both forms have the EXACT same button...and both forms are of the same normal form type.

Any help is highly appreciated.

Usmaan~

Recommended Answers

All 2 Replies

When you hit the X in the corner, the form closes, it doesn't exit the application. But the startup form is run from Program.cs which has this line Application.Run(new Form1()); . When the form closes, the line following this one is run, and there is nothing there so the application exits.

I suspect your second form is opened by the first form. So when it closes control is returned (effectively) to the first form which hasn't been told to close.

If you want form2 to close the application add a handler to the FormClosing event and add the code Application.Exit() .

For debugging purposes, I have temporarily made a button called cmdQuit which looks like this:

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

I had that before I posted this thread. How do I change it so it recognizes that I hit the X button on the form rather than using this quit button then? I know the syntax of course which is as you mentioned.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.