C# 表格关闭事件

C# 表格关闭事件,c#,winforms,visual-studio,C#,Winforms,Visual Studio,我的应用程序在c#中关闭时遇到问题。当我按下关闭按钮时,它会在消息框中显示两次或更多次。我该怎么办 private void home_FormClosed(object sender, FormClosedEventArgs e) { DialogResult dialog = MessageBox.Show("Are you sure you want to really exit ? ", "Exit",

我的应用程序在c#中关闭时遇到问题。当我按下关闭按钮时,它会在消息框中显示两次或更多次。我该怎么办

private void home_FormClosed(object sender, FormClosedEventArgs e)
{
    DialogResult dialog = MessageBox.Show("Are you sure you want to really exit ? ", 
                            "Exit", 
                             MessageBoxButtons.YesNo, 
                             MessageBoxIcon.Question);
    if (dialog == DialogResult.Yes)
    {
        System.Windows.Forms.Application.Exit();
    }
    else if (dialog == DialogResult.No)
    {
        this.Show();
    }
}

使用
Form1\u FormClosing
事件,也不要像这样使用
Application.Exit()

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    var x = MessageBox.Show("Are you sure you want to really exit ? ", 
                             "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

    if (x == DialogResult.No) 
    {
       e.Cancel = true;
    }
    else
    {
      e.Cancel = false;
    }
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
   e.Cancel = MessageBox.Show("Are you sure you want to really exit ? ", 
              "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No;
}
或者像这样:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    var x = MessageBox.Show("Are you sure you want to really exit ? ", 
                             "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

    if (x == DialogResult.No) 
    {
       e.Cancel = true;
    }
    else
    {
      e.Cancel = false;
    }
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
   e.Cancel = MessageBox.Show("Are you sure you want to really exit ? ", 
              "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No;
}

您应该使用
Form.FormClosing
事件,而不是
FormClosing
事件。在参数中,可以找到一个字段
e.Cancel
。通过将此设置为false,您可以保持表单处于打开状态

您可以通过检查
Application.Exit()
是否已从
FormClosing
事件中调用
来避免出现多个提示:

    void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (e.CloseReason != CloseReason.ApplicationExitCall)
        {
            DialogResult dialog = MessageBox.Show("Are you sure you want to really exit ? ", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialog == DialogResult.Yes)
            {
                System.Windows.Forms.Application.Exit();
            }
            else if (dialog == DialogResult.No)
            {
                e.Cancel = true;
            }
        }
    }

改为使用表单关闭事件

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        var confirmation = MessageBox.Show("Sure to close form", "Confirm", MessageBoxButtons.YesNo);

        if (confirmation == System.Windows.Forms.DialogResult.No)
        {
             e.Cancel = true; //Even cancelled, form will not get closed now
        }
    }

我们可能需要更多的细节来回答这个问题。它过去对我很管用。我想您多次添加事件委托,系统会调用您添加的每个事件委托。在我的应用程序中,我创建了三个windows窗体。我使用以下代码分别关闭所有表单…您不应该使用FormClosing而不是FormClosed吗?显示您如何订阅
home\u FormClosed
的代码显示在上面。感谢您的回复。但我使用的是VisualStudio2012。所以没有关键字“取消”。但这个关键词出现在Visual c#2012 Express中……该字段是在.Net 2.0中引入的。()所以visual studio 2012也应该有Cancel属性。感谢您的回复,但在我的visual studio c#版本中,没有名为“Cancel”的定义或异常方法。。。它还说我丢失了一个指令或程序集引用。那么它应该在那里,因为FormClosing从.NET2.0开始就存在了。你的问题在别的地方。提供更多关于项目设置和如何使用所有代码的详细信息…我将FormClose更改为FormClosing。但是有一个错误是“System.Windows.Forms.FormClosedEventHandler中的FormClosing没有重载”…还没有。。。取消关键字不在我的visual studio c#应用程序中