簡體   English   中英

如何使用 C# 窗口窗體對控制按鈕進行退出確認是或否

[英]How to make Exit confirmation yes or no on Control Button using C# Window Form

我的窗口應用程序中有一個問題,有兩種形式:登錄和主。

我在兩個表單上都創建了退出按鈕,並且還對退出確認進行了編碼。

當我們按表單 1 上的退出按鈕消息 No 時,它工作正常。但是我們登錄表單以

另一個表單和第二個表單退出按鈕消息按鈕按否,然后返回返回

表格一然后點擊退出按鈕 否 :::::: 它將顯示兩次確認消息彈出......................

代碼

private void Btn_Exit_Click(object sender, EventArgs e) 
{
    DialogResult dr = MessageBox.Show("Do you want to exit.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question); 
    if (dr == DialogResult.Yes) 
    { 
       Application.ExitThread(); 
    } else 
    { } 
}

請在 c# 中有任何解決方案…………

在您的情況下,請考慮LoginFormMainForm ,登錄后您嘗試隱藏LoginForm並顯示MainForm 問題是當您嘗試關閉MainForm它會調用相應的FormCloseEvent ,一旦您選擇關閉,它會自動調用隱藏在后台的父表單,因此它會調用LoginForm's FormCloseEvent 這就是兩次彈出窗口的原因。

要解決這個問題,您需要觸發一個事件,即每當子窗體關閉時,您需要引發一個標志,因此在您父母的FormCloseEvent您需要檢查標志,如果標志為真,則無需顯示彈出窗口。

    private bool isMainFormClosed = false;

    private void showMainForm()
    {
        // Hide the loginform UI.
        this.Hide();

        var mainForm = new MainForm();

        // Creating close event for mainform, whenever close icon is clicked it will close the login form which is hiding in background.
        mainForm.FormClosed += new FormClosedEventHandler(mainFormClosed);

        // Show the mainform UI
        mainForm.Show();
    }

    private void mainFormClosed(object sender, FormClosedEventArgs e)
    {
        this.isMainFormClosed = true;

        this.Close();
    }

    private void loginFormClosing(object sender, FormClosingEventArgs e)
    {   
        if(!this.isMainFormClosed)
        {
            DialogResult dialogResult = MessageBox.Show("Do you want to close the application",AppConstants.APPLICATION_NAME,
                                                        MessageBoxButtons.YesNo,MessageBoxIcon.Question);

            if(dialogResult == DialogResult.No) 
                e.Cancel = true;
        }
    }

就這樣編碼。

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
        if (MessageBox.Show("Do you want to close this application?", "Exit", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
        {
            e.Cancel = true;
        }
}

DialogResultdialogs后由dialogs返回。
它指示用戶在對話框上單擊了哪個按鈕。

代碼

private void btnExit_Click(object sender, EventArgs e)
{
    const string message = "Do you want to exit?";
    const string caption = "EXIT";
    var result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
    if (result == DialogResult.Yes)
    {
       Application.Exit();
    }
    else if (result == DialogResult.Yes)
    {
       this.Close();
    }
}
    private void button1_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (MessageBox.Show("Do you want to close this application?", "Exit", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
        {
            e.Cancel = true;
        }
    }

暫無
暫無

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

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