简体   繁体   English

关闭多个表单 C#

[英]Closing multiple forms C#

I'm currently working with C# windows forms.我目前正在使用 C# windows 窗体。 I got like 40 cs files, and when the applications works like : Pressing a button -> Opens new form upon the first one and pressing another button opens another form upon the one before.我得到了 40 个 cs 文件,当应用程序工作时:按一个按钮 - > 在第一个表单上打开新表单,然后按另一个按钮在前一个表单上打开另一个表单。

Now, whenever I click multiple forms none of the others are closing itself when I'm pressing different buttons, they all stay on the background.现在,每当我单击多个表单时,当我按下不同的按钮时,其他表单都不会自行关闭,它们都留在背景中。

Now, if I use this.Close();现在,如果我使用 this.Close(); its working with 1, but i got like 40 cs files and it's hard to compile them all..它使用 1,但我得到了 40 个 cs 文件,很难将它们全部编译。

looking for any suggestion ?寻找任何建议? Thank you guys for any sort of help !谢谢你们的任何帮助!

public partial class Costumers_Orders : Form
{
    public Costumers_Orders()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        NewCostumer mm = new NewCostumer();
        mm.Show();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Remove_Customer mm = new Remove_Customer();
        mm.Show();
    }

    private void Costumers_Orders_Load(object sender, EventArgs e)
    {

    }

    private void button3_Click(object sender, EventArgs e)
    {
        Show_Edit_Customer mm = new Show_Edit_Customer();
        mm.Show();
    }

    private void button4_Click(object sender, EventArgs e)
    {
        Orders_report mm = new Orders_report();
        mm.Show();
    }
}

Assuming you only ever want one form open at a time, and that "Costumers" is the main form and can't be closed:假设您一次只希望打开一个表单,并且“客户”是主要表单并且无法关闭:

private void button1_Click(object sender, EventArgs e)
{
    ShowForm(new NewCostumer());
}

private void ShowForm(Form newForm)
{
    List<Form> forms = new List<Form>();
    foreach (Form frm in Application.OpenForms)
    {
        if (!(frm is Costumers))
        {
            forms.Add(frm);
        }
    }
    foreach (Form frm in forms)
    {
        frm.Close();
    }
    newForm.Show();
}

There are other ways you could do this as well.还有其他方法可以做到这一点。

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

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