簡體   English   中英

在計時器工作時退出程序時,如何進行兩次退出消息確認?

[英]How do i make double quit message confirmation when exit the program while timer is working?

現在是我的代碼:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                if (MessageBox.Show("Are you Sure you want to Exit. Click Yes to Confirm and No to continue", "WinForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    if (timer2.Enabled == true)
                    {
                        if (MessageBox.Show("Quit now will delete all the file of the current operation. Click Yes to Confirm and No to continue", "WinForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                        {
                            e.Cancel = true;
                        }
                        else
                        {
                            e.Cancel = false;
                            return;
                        }
                    }
                    e.Cancel = true;
                }
                else
                {
                    e.Cancel = false;
                }
            }
        }

我想做的是,如果未啟用timer2,然后退出,請用戶退出或不正常。

但是,如果確實啟用了timer2,則首先詢問是否退出,然后詢問第二個內部問題“立即退出將刪除當前操作的所有文件”,如果用戶在第二個問題上單擊“是”,則執行某些操作(刪除文件),然后放棄。 但是,如果用戶在第二個問題上單擊“否”,則什么也不會使程序運行。

但是現在,如果我在第一個問題上單擊“否”,它現在將無法正常工作,然后如果timer2啟用為true,它將詢問第二個問題。 如果timer2為true,並且我在第一個問題上單擊“是”,它將退出程序而不會詢問第二個問題。

一團糟。

我希望我啟用的timer2為真,請問第二個問題:在第二個問題上,用戶做了YES刪除文件,則需要執行任何操作,然后關閉所有內容。 在第二個問題上,用戶沒有返回並保持程序正常運行。

如果在第一個問題上啟用的timer2為false,則在第一個問題上,如果用戶執行“是”,則退出程序;如果用戶執行“否”,則使程序保持正常運行。

您只想問用戶第二個問題,是否仍啟用計時器? 所以做以下

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (e.CloseReason == CloseReason.UserClosing)
        {
            if (MessageBox.Show("Are you Sure you want to Exit. Click Yes to Confirm and No to continue", "WinForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (timer2.Enabled == true)
                {
                    if (MessageBox.Show("Quit now will delete all the file of the current operation. Click Yes to Confirm and No to continue", "WinForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        //do your work here like delete files etc
                    }
                    else
                    {
                        e.Cancel = false;
                        return;
                    }
                }
                e.Cancel = true;
            }
            else
            {
                e.Cancel = false;
            }
        }
    }

您的代碼可以通過兩種方式簡化:

  • 不要設置e.Cancel = false那是原始值,沒有必要進行設置。
  • 反轉條件並盡早return -這樣可以減少復雜的嵌套。

另外,我認為您想檢查第一個MessageBox的DialogResult.Yes ,而不是No

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason != CloseReason.UserClosing)
        return;    // Not closing - we don't care.

    var res = (MessageBox.Show("Are you Sure you want to Exit? Click Yes to Confirm and No to continue",
        "WinForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

    if (res != DialogResult.Yes) {
        // User didn't say Yes - don't exit.
        e.Cancel = true;
        return;   
    }

    if (timer2.Enabled == true)
    {
        // Only ask this question if timer2 is running.
        res = MessageBox.Show("Quit now will delete all the file of the current operation. Click Yes to Confirm and No to continue",
            "WinForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

        if (res != DialogResult.Yes) {
             e.Cancel = true;
             return;    // User didn't say Yes - don't exit.
        }
     }

     // Quit
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM