簡體   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