单击“X”按钮时要求确认

时间:2011-12-03 12:18:58

标签: c# winforms button

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

    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);
    }

5 个答案:

答案 0 :(得分:23)

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

将最后一位替换为:

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

e.Cancel = (result == DialogResult.No);

答案 1 :(得分:6)

e.Cancel上的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;
    }

答案 2 :(得分:2)

就这么简单

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;
        }

答案 3 :(得分:2)

这个问题现在已经过时了,但这种方式更简单,更短,我认为这对于到达此页面的人来说非常有用:

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;
    }
}

并使用其他地方this.Close()而不是函数。

答案 4 :(得分:1)

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'点击)。也许你可以做这样的事情(伪代码):

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