【问题标题】:Canceling closing of C# Form取消关闭 C# 窗体
【发布时间】:2013-03-01 23:13:55
【问题描述】:

我的程序有两种关闭方式,一种是右上角的“X”,另一种是“退出”按钮。现在,当满足某个条件时按下其中任何一个时,会弹出一条消息,通知用户他们尚未保存。如果他们确实保存了,则不会弹出消息,并且程序会正常关闭。现在,当消息弹出时,用户会得到一个带有 Yes 和 No 按钮的 MessageBox。如果按下“是”,则程序需要保存。如果按下“否”,则程序需要取消在用户按下“X”或“退出”按钮时已启动的关闭事件。

最好的方法是什么?

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    TryClose();
}

private void TryClose()
{
    if (saved == false)
    {
        //You forgot to save
        //Turn back to program and cancel closing event
    }
}

【问题讨论】:

标签: c#


【解决方案1】:

FormClosingEventArgs 包含一个 Cancel 属性。只需设置e.Cancel = true; 以防止表单关闭。

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (!saved)
        e.Cancel = true;
}

编辑以响应 cmets:

由于您的目标是允许使用相同的“保存方法”,因此我会将其更改为在成功时返回 bool

private bool SaveData()
{
     // return true if data is saved...
}

然后你可以写:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    // Cancel if we can't save
    e.Cancel = !this.SaveData();
}

您的按钮处理程序等仍然可以根据需要调用SaveData()

【讨论】:

  • 关键是保存功能是从方法中嵌入的,并且该方法没有“FormClosingEventArgs e”属性。更多按钮或事件使用保存功能。
【解决方案2】:

这将满足您的需求:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = !TryClose();
}

private bool TryClose()
{
    return DialogResult.Yes == MessageBox.Show("Are you sure?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
}

【讨论】:

  • 当可以覆盖时,您不应该使用事件。
【解决方案3】:

使用事件参数的Cancel property,设置为true取消。

【讨论】:

    【解决方案4】:

    要取消关闭事件,只需在 FormClosingEventArgs 实例上将 Cancel 属性设置为 true

    if (!saved) {
      // Message box
      e.Cancel = true;
    }
    

    【讨论】:

      【解决方案5】:

      您可以从退出按钮调用 Close。然后像其他人所说的那样在 Forms.FormClosing 事件中处理关闭。这将处理退出按钮单击和从“X”关闭的表单

      【讨论】:

        【解决方案6】:

        重写 OnFormClosing:

         protected override void OnFormClosing(FormClosingEventArgs e)
         {
            if (saved == true)
            {
               Environment.Exit(0);
            }
            else /* consider checking CloseReason: if (e.CloseReason != CloseReason.ApplicationExitCall) */
            {
               //You forgot to save
               e.Cancel = true;
            }
            base.OnFormClosing(e);
         }
        

        【讨论】:

          【解决方案7】:
              private void Form1_FormClosing(object sender, FormClosingEventArgs e)
              {
                  e.Cancel = !TryClose();
              }
          
              private bool TryClose()
              {
                  if (!saved)
                  {
                      if (usersaidyes)
                      {
                          // save stuff
                          return true;
                      }
                      else if (usersaidno)
                      {
                          // exit without saving
                          return false;
                      }
                      else
                      {
                          // user cancelled closing
                          return true;
                      }
                  }
                  return true;
              }
          

          【讨论】:

          • 当可以覆盖时,您不应该使用事件。
          猜你喜欢
          • 1970-01-01
          • 2011-08-30
          • 2022-12-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多