1

我有MDI form一些儿童表格需要在关闭之前显示一个消息框,而其他表格可以在不询问的情况下关闭。由于application.Exit()close event子窗体调用时出现问题,我处理close event父窗体并检查它的触发位置。如果它是在需要消息框的表单中触发的,我会调用它,否则只需关闭应用程序。所有这些都在这段代码中实现:

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            SEdit se       = this.ActiveMdiChild as SEdit;
            SoEdit soleEdit = this.ActiveControl as SoEdit;
            UppEdit ue      = this.ActiveControl as UpEdit;
            MEdit mat  = this.ActiveControl as MEdit;
            LEdit lse      = this.ActiveControl as LEdit;
            CEdit cle    = this.ActiveControl as CEdit;

            if (se != null || soleEdit != null || ue != null || mat != null || lse != null || cle != null)
            {
                if (MessageBox.Show("Do you want to save before exit?", "Closing",
                      MessageBoxButtons.YesNo,
                      MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    MessageBox.Show("To Do saved.", "Status",
                              MessageBoxButtons.OK,
                              MessageBoxIcon.Information);
                }
            }
         }

我还在学习,但我知道这么长的 if 语句是错误代码的标志,但我不知道如何改进它。处理这种情况的正确方法是什么?

4

5 回答 5

6

提取条件到分离方法:

private bool AnyChildAlive()
{
   return (this.ActiveMdiChild is SEdit) ||
          (this.ActiveControl is SoEdit) ||
          ...
          (this.ActiveControl is CEdit);
}

然后调用这个方法(也使用保护条件来避免嵌套 if 语句):

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{          
    if (!AnyChildAlive())
       return;

    if (MessageBox.Show("Do you want to save before exit?", "Closing",
           MessageBoxButtons.YesNo, 
           MessageBoxIcon.Information) != DialogResult.Yes)
       return;

   MessageBox.Show("To Do saved.", "Status",
        MessageBoxButtons.OK, MessageBoxIcon.Information);     

}
于 2013-02-01T15:10:06.873 回答
2

可能最好的方法是创建一个接口,如:

public interface IFormActions {
  bool AskBeforeClosing();
  void SaveData();
}

然后,为每个表单实现该接口,并在 MainForm_FormClosing 方法中执行以下操作:

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
  IFormActions se = this.ActiveControl as IFormActions;
  if ((se != null) && se.AskBeforeClosing()) {
    if (MessageBox.Show("Do you want to save before exit?", "Closing", MessageBoxButtons.YesNo,  MessageBoxIcon.Information) == DialogResult.Yes) {
      se.SaveData();
      MessageBox.Show("Saved", "Status", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
  }
}

由于这是如何编写的,您不必为所有表单实现接口,只需为您真正想要询问结束问题的那些表单实现接口。

于 2013-02-01T15:10:24.863 回答
1

为了使其更具吸引力或重复使用,您可能需要考虑将验证转移到不同的方法。

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
    if (FormIsValid())
    {
        if (MessageBox.Show("Do you want to save before exit?", "Closing",
              MessageBoxButtons.YesNo,
              MessageBoxIcon.Information) == DialogResult.Yes)
        {
            MessageBox.Show("To Do saved.", "Status",
                      MessageBoxButtons.OK,
                      MessageBoxIcon.Information);
        }
    }
}

private bool FormIsValid()
{
    return 
    (this.ActiveMdiChild as SEdit) != null ||
    (this.ActiveControl as SoEdit) != null ||
    (this.ActiveControl as UpEdit) != null ||
    (this.ActiveControl as MEdit)  != null ||
    (this.ActiveControl as LEdit)  != null ||
    (this.ActiveControl as CEdit)  != null;
}
于 2013-02-01T15:09:57.057 回答
0

不是最好的方法,但您可以将Tag表单的属性设置为 1 或 0 并检查:

          if (this.ActiveControl.Tag == 1)
            {
                if (MessageBox.Show("Do you want to save before exit?", "Closing",
                      MessageBoxButtons.YesNo,
                      MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    MessageBox.Show("To Do saved.", "Status",
                              MessageBoxButtons.OK,
                              MessageBoxIcon.Information);
                }
            }
于 2013-02-01T15:16:41.460 回答
0
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
  var objects = new List<object>
                {
                    this.ActiveMdiChild as SEdit,
                    this.ActiveControl as SoEdit,
                    this.ActiveControl as UpEdit,
                    this.ActiveControl as LEdit,
                    this.ActiveControl as CEdit
                };

        if (objects.Any(x => x != null))
        {
            if (MessageBox.Show("Do you want to save before exit?", "Closing",
                  MessageBoxButtons.YesNo,
                  MessageBoxIcon.Information) == DialogResult.Yes)
            {
                MessageBox.Show("To Do saved.", "Status",
                          MessageBoxButtons.OK,
                          MessageBoxIcon.Information);
            }
        }
     }
于 2013-02-01T15:11:05.143 回答