简体   繁体   English

关闭所有应用程序,然后单击X Winform。

[英]Close All application click X Winform

I'm trying to change the names of the datagrid and labels in Form2 from Form1 based on the selection. 我试图根据选择从Form1更改Form2中的数据网格和标签的名称。

private void button1_Click_1(object sender, EventArgs e)
        {
            this.Hide();

            Form2 frm = new Form2();
            frm.Show();


        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();

            frm.dataGridView1.Columns["FirstName"].HeaderText = "Prenom";
            frm.dataGridView1.Columns["LastName"].HeaderText = "Nom";


            this.Hide();
            frm.Show();
        }

The above approach is working fine for me but I have a problem. 上面的方法对我来说很好,但是我有一个问题。 When I click on X in the second/Form 2 it's just closing the Form2 not the Form1 . 当我在第二个/ Form 2中单击X ,它只是关闭Form2而不是Form1 How can close all the application when i click on the X . 当我单击X时,如何关闭所有应用程序。

Is there any better way of doing this??? 有没有更好的办法做到这一点??? The reason why i'm not using I'm using Telerik and I don't find any option to add resource file in that. 我不使用的原因是使用Telerik,但没有找到任何添加资源文件的选项。 Please correct me if i'm wrong. 如果我错了,请纠正我。 Thank you. 谢谢。

You simply need to attach an event handler to the Closed event of the newly created form so that it closes the main form when it's closed: 您只需要将事件处理程序附加到新创建的表单的Closed事件上,以便在关闭主窗体时将其关闭:

private void button1_Click_1(object sender, EventArgs e)
{
    this.Hide();

    Form2 frm = new Form2();
    frm.FormClosed += (_, args) => this.Close(); //Added this method
    frm.Show();
}

Add that same method to the other click handler as well. 同样,将相同的方法添加到其他点击处理程序中。

The easiest way to close an application is to call the static method: Application.Exit 关闭应用程序的最简单方法是调用静态方法: Application.Exit

Another method if you only want to close one single form would be to add a handler for the FormClosing event and close the main form there. 如果您只想关闭一个表单,则另一种方法是为FormClosing事件添加一个处理程序,然后在其中关闭主表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM