繁体   English   中英

C#关闭所有表格

[英]C# close all forms

我的程序有问题。

我有3个表单:第一个表单打开第二个表单。 第二个打开第三个表单或返回第一个表单。 第三种形式可以打开第一种或第二种形式。

这是我打开第二张表格的方式:

private void Open_second_form()
    {
        Form2 myForm = new Form2(Type_person);
        this.Hide();
        myForm.ShowDialog();
        this.Close();
    }

我打开的其他表格完全一样。

这是关于我如何关闭表单的代码。 每个表格都有这个方法:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (MessageBox.Show("Exit or no?",
                           "My First Application",
                            MessageBoxButtons.YesNo,
                            MessageBoxIcon.Information) == DialogResult.No)
        {
            this.Close();
            Environment.Exit(1);
        }
    }

当我打开第三个表单时,我得到3个MessagesBoxes。 如果我打开第一个表单,我只得到一个MessageBox。

我想在只获得一个MessageBox时关闭所有表单。

我尝试了很多解决方案,但都没有效果。 我试过Application.exit();

请帮我 :)

您的确认消息很有趣,结果不明显= D.

有两种解决方案可以解决您的问题。

1)如果用户选择关闭应用程序 - 不再显示确认

private static bool _exiting;

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (!_exiting && MessageBox.Show("Are you sure want to exit?",
                       "My First Application",
                        MessageBoxButtons.OkCancel,
                        MessageBoxIcon.Information) == DialogResult.Ok)
    {
        _exiting = true;
        // this.Close(); // you don't need that, it's already closing
        Environment.Exit(1);
    }
}

2)使用CloseReason仅确认用户操作

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing)
    {
        if(MessageBox.Show("Are you sure want to exit?",
                       "My First Application",
                        MessageBoxButtons.OkCancel,
                        MessageBoxIcon.Information) == DialogResult.Ok)
            Environment.Exit(1);
        else
            e.Cancel = true; // to don't close form is user change his mind
    }

}

调用Environment.Exit(0); 方法

 private void btnExit_Click(object sender, EventArgs e)
{
    Environment.Exit(0);
}

我总是在菜单上使用这个片段。 我希望这有帮助。

for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
{
    if (Application.OpenForms[i].Name != "Menu")
    Application.OpenForms[i].Close();
}

对于表单1,考虑按钮称为btnForm1To2

private void btnForm1To2_Click(object sender, EventArgs e)
{
    using (Form2 myForm = new Form2(Type_person))
    {
        this.Hide();
        myForm.ShowDialog();
        this.Show();
    }
}

在FORM 2中,考虑到第1页的按钮是btnForm2To1,第3页的按钮是btnForm2To3

private void btnForm2To1_Click(object sender, EventArgs e)
{
    // Just close the form 2, as it was called from form 1, this form will be displayed
    this.DialogResult = DialogResult.OK;
}

private void btnForm2To3_Click(object sender, EventArgs e)
{
    using (Form3 myForm = new Form3(...))
    {
        this.Hide();
        DialogResult form3result = myForm.ShowDialog();

        // Now handle the results from form 3 to navigate to form 2 or 1
        // In my example, form 3 replies Abort to go back to 1. On cancel (or other dialog results) we will stay on form 2
        if (DialogResult.Abort.Equals(form3result))
        {
            this.DialogResult = DialogResult.OK;
        }
        else
        {
            this.Show();
        }
    }
}

在FORM 3中,考虑第1页的按钮是btnForm3To1,第2页的按钮是btnForm3To2

private void btnForm3To1_Click(object sender, EventArgs e)
{
    this.DialogResult = DialogResult.Abort;
}

private void btnForm3To2_Click(object sender, EventArgs e)
{
    this.DialogResult = DialogResult.OK;
}

如果您希望用户使用MessageBox确认他的点击,只需将它们放在每个按钮单击方法中的this.DialogResult之前

请注意 ,这种方式比Environment.Exit(0); Environment.Exit(0);

WPF

在( ###Window.xaml )中设置窗口Closing事件

Closing="Window_Closing"

然后在( ###WWindow.xaml.cs

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    foreach(Window w in Application.Current.Windows)
        if(w != this)
            w.Close();
}

Windows窗体

  • 转到Form#.cs [Design] >>表单#属性

  • 然后单击“事件”

  • 并双击FormClosing

在( Form#.cs

如果您从“ 启动”表单中关闭,则最好使用此代码。

 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
 {
     for(int i = 1; i < Application.OpenForms.Count; ++i)
         Application.OpenForms[i].Close();
 }

其他适用于任何形式

 private void Form7_FormClosing(object sender, FormClosingEventArgs e)
 {
     for(int i = 0; i < Application.OpenForms.Count; ++i)
         if(Application.OpenForms[i] != this)
                Application.OpenForms[i].Close();
 }

如果你想关闭除主窗体之外的所有窗体(特别是对话窗口),我发现这个功能很有用。

        public static void closeAll()
    {
        FormCollection fc = Application.OpenForms;
        if (fc.Count > 1)
        {
            for (int i = (fc.Count); i > 1; i--)
            {
                Form selectedForm = Application.OpenForms[i - 1];
                selectedForm.Close();
            }
        }
    }
Application.Current.Shutdown();

这就是我在需要关闭整个应用程序时使用的内容。 它关闭所有形式,非常适合关闭所有内容。 我不知道这是不是你想要的。

暂无
暂无

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

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