简体   繁体   中英

To close C# Forms Application

我有2个表单...当我启动应用程序时...并使用标题栏中的关闭“X”整个应用程序关闭...现在当我从第一个表单中选择一个选项时,我的情况下它是一个按钮“添加“作为它的电话簿应用程序..它转到第二种形式,因为我使用了firstform.hide()和2ndform.show()...现在,当我从标题栏中执行”X“时,它并没有完全关闭,因为1stform没有关闭....如何在整个应用程序关闭的任何阶段以这种方式对其进行编程

Your first form is set as the startup form. That means whenever it gets closed, your entire application is closed. And conversely, your application does not close until it gets closed. So when you hide the startup form and show the second form, the user closing the second form does not trigger your application closing because they have only closed a secondary, non-modal dialog.

I recommend changing your design so that the startup form is also the main form of your application. No sense trying to work around built-in functionality that can actually be useful. You want the application to quit when the main form is closed, no matter what other child forms are opened.

But the quick-and-dirty solution in your case is to make a call to Application.Exit . That will close all of the currently open forms and quit your application immediately. As I said just above, I don't so much recommend this approach because having to call Application.Exit from every form's FormClosed event handler is a sign that something has gone seriously wrong in your design.

If the single startup form paradigm doesn't work out for you, you should look into taking matters into your own hands and customizing the Main method in your Program.cs source file. See the answers given to this related question for some ideas on how that might work for you.

What you can do is to use the Form's FormClosing event, and add the following code:

Application.Exit();

This will stop the entire application, and close all windows. However, if a background thread is running, the process itself will survive. In this case you can use:

Environment.Exit();

Add a Application.Exit on every forms's Closing event

like this:

Create an closing event handler first

private void Form_ClosingEventhandler()(object sender, CancelEventArgs e)
{
      //Perform any processing if required like saving user settings or cleaning resources
      Application.Exit();
}

then bind this event to any form you create.

//Where you create new form and show it.
Form1 frm= new Form1();
//set other properties
frm.Closing += new EventHandler(Form_ClosingEventhandler);
Form2 frm2= new Form2();
//set other properties
frm2.Closing += new EventHandler(Form_ClosingEventhandler);

Surely you don't want to shut down the entire application after the user adds a phone number? You just need to make sure that your main window becomes visible again. Write that like this:

    private void AddButton_Click(object sender, EventArgs e) {
        var frm = new AddPhoneNumber();
        frm.StartPosition = FormStartPosition.Manual;
        frm.Location = this.Location;
        frm.Size = this.Size;   // optional
        frm.FormClosing += delegate { this.Show(); };
        frm.Show();
        this.Hide();
    }

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