Asking for confirmation when "X" button is clicked

本文关键字:quot button is clicked for confirmation Asking when | 更新日期: 2023-09-27 18:16:39

问题是,确实会弹出带有"确定要关闭吗?"的消息框,但当我单击"否"时,它仍然会继续关闭程序。有什么建议吗?这是我的代码:

    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        CloseCancel();
    }
    public static void CloseCancel()
    {
        const string message = "Are you sure that you would like to cancel the installer?";
        const string caption = "Cancel Installer";
        var result = MessageBox.Show(message, caption,
                                     MessageBoxButtons.YesNo,
                                     MessageBoxIcon.Question);
        if (result == DialogResult.Yes)
            Environment.Exit(0);
    }

Asking for confirmation when "X" button is clicked

当您要求取消关闭操作时,需要将FormClosingEventArgs参数的Cancel属性设置为true。显式Environment.Exit(0)通常不需要,因为表单正在以任何方式关闭(关闭过程的取消是选择加入,而不是选择退出(。

将最后一位替换为:

var result = MessageBox.Show(message, caption,
                             MessageBoxButtons.YesNo,
                             MessageBoxIcon.Question);
e.Cancel = (result == DialogResult.No);

e。取消FormClosing事件将停止关闭过程

    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        if (CloseCancel()==false)
        {
            e.Cancel = true;
        };
    }
    public static bool CloseCancel()
    {
        const string message = "Are you sure that you would like to cancel the installer?";
        const string caption = "Cancel Installer";
        var result = MessageBox.Show(message, caption,
                                     MessageBoxButtons.YesNo,
                                     MessageBoxIcon.Question);
        if (result == DialogResult.Yes)
            return true;
        else
            return false;
    }

这个问题现在很老了,但这种方法更简单、更简短,我认为它对那些到达这个页面的人有用:

protected override void OnFormClosing(FormClosingEventArgs e)
{
    if (MessageBox.Show("Are you sure that you would like to cancel the installer?", "Cancel Installer", MessageBoxButtons.YesNo) == DialogResult.No)
    {
        e.Cancel = true;
    }
}

并且在其他地方使用CCD_ 5而不是函数。

只需简单的

protected override void OnFormClosing(FormClosingEventArgs e)
        {            
            base.OnFormClosing(e);
            if (PreClosingConfirmation() == System.Windows.Forms.DialogResult.Yes)
            {
                Dispose(true);
                Application.Exit();
            }
            else
            {
                e.Cancel = true;
            }
        }
        private DialogResult PreClosingConfirmation()
        {
            DialogResult res = System.Windows.Forms.MessageBox.Show(" Do you want to quit?          ", "Quit...", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            return res;
        }
protected Boolean CanClose(Boolean CanIt)
{
   if(MessageBox.Show("Wanna close?", "Cancel Installer", MessageBoxButtons.YesNo,       MessageBoxIcon.Question).ShowDialog() == DialogResult.Yes)
   {
      // Yes, they want to close.
      CanIt = true;
   }
   else
   {
     // No, they don't want to close.
     CanIt = false;
   }
   return CanIt;
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
    if(CanClose(false) == true)
    {
       this.Dispose(true);
    }
    else
    {
       e.Cancel = true;
    }
}

我不确定如何处理你提到的另一种情况(处理"X"点击(。也许你可以这样做(psuedo代码(:

// IF CLICKED YES
   THEN CLOSE
// ELSE-IF CLICKED NO
   THEN DO NOTHING
// ELSE
   THEN DO NOTHING

这对我有效:

private void FormName_FormClosing(object sender, FormClosingEventArgs e)
    {
        DialogResult dialog = MessageBox.Show("Do you really want to close ?", "CLOSE", MessageBoxButtons.YesNo, MessageBoxIcon.Stop);
        if (dialog == DialogResult.No)
        {
            e.Cancel = true;
        }
        else
        {
            //code to run before close
        }
       
    }