2013-02-07 26 views
0

我有一个Windows Form项目,它使用MDI。我有一个方法负责保存任何可打开的可编辑窗体的数据,并为不同的事件调用此方法。但我也在父母的表格before close事件中使用它,我需要检查所有打开的MDIchild,如果它们之间有可编辑的表单,并且如果有,请求保存。除此之外,我只关心ActiveMdiChild是否可编辑,并要求仅为其保存。一种方法有两个几乎相同的if条件;是否有可能优化代码

下面是做好这项工作的方法:

protected void AskForSaveBeforeClose(object sender) 
{ 
    //Get the active child 
    BaseForm activeChild = this.ActiveMdiChild as BaseForm; 
    //Casting to MainForm return null if the sender is child form 
    Form mainForm = sender as MainForm; 
    //If the before close event comes from the parent loop all forms 
    if (mainForm != null) 
      { 
     foreach (BaseForm f in MdiChildren) 
     { 
      if (f.isEditable == true) 
      { 
       if (MessageBox.Show("To Do Do You Want To Save from MainForm " + f.Text, "Status", 
         MessageBoxButtons.YesNo, 
         MessageBoxIcon.Information) == DialogResult.Yes) 
       { 
        f.Save(); 
       } 
      } 
     } 

      } 
    //if the event is not from the parent's before close just ask for the active child 
    else if (mainForm == null && activeChild != null) 
    { 
     if (activeChild.isEditable == true) 
     { 
      if (MessageBox.Show("To Do Do You Want To Save from AC ", "Status", 
         MessageBoxButtons.YesNo, 
         MessageBoxIcon.Information) == DialogResult.Yes) 
      { 
       activeChild.Save(); 
      } 
     } 
    } 
} 

BaseForm是一种形式,大家甚至继承父窗体。现在我已经完成了将代码放在一个方法中,所以现在我只调用这个方法,但困扰我的是这两个部分几乎完全相同,但我仍然无法弄清楚如何优化逻辑。

回答

2

您也可以创建集合,然后循环。

protected void AskForSaveBeforeClose(object sender) { 
    //Get the active child 
    BaseForm activeChild = this.ActiveMdiChild as BaseForm; 
    //Casting to MainForm return null if the sender is child form 
    Form mainForm = sender as MainForm; 

    //Create collection to loop through 
    List<BaseForm> formsToCheck = new List<BaseForm>(); 
    if (mainForm != null && MdiChildren != null && MdiChildren.Any()) 
     formsToCheck.AddRange(MdiChildren); 
    if (mainForm == null && activeChild != null) 
     formsToCheck.Add(activeChild); 

    // Only check editable forms 
    formsToCheck = formsToCheck.Where(f => f.IsEditable).ToList(); 

    // Loop through forms 
    foreach (BaseForm f in formsToCheck) { 
     var fromText = "MainForm " + f.Text; 
     if (f == activeChild) 
      fromText = "AC"; 
     if (MessageBox.Show("To Do Do You Want To Save from " + fromText, "Status", 
       MessageBoxButtons.YesNo, 
       MessageBoxIcon.Information) == DialogResult.Yes) { 
      f.Save(); 
     }  
    } 
} 
+0

谢谢,会试试这个! – Leron

4

通过使用函数。将这些代码放入函数中,根据需要进行参数化。

方案:

void askToSave (Baseform f) { 
    if (f.isEditable == true) 
    { 
     if (MessageBox.Show("To Do Do You Want To Save from MainForm " + f.Text, "Status", 
       MessageBoxButtons.YesNo, 
       MessageBoxIcon.Information) == DialogResult.Yes) 
     { 
      f.Save(); 
     } 
    } 
} 

,你可以摆脱if语句的嵌套:

if (f.isEditable == true && 
     MessageBox.Show("To Do Do You Want To Save from MainForm " + f.Text, .... 

这工作,因为&&(如||)是短路的运营商,这意味着,一旦条件被伪造,从左到右,其余的操作数就不会被评估。

0

你可以收集你的有效形式进入名单,其中T可实现所有你需要为你的业务逻辑的东西的接口(也许是列表本身就足够了)。元素(表单)应该添加到这个列表中,然后在随后的循环中,您可以遍历该列表,显示消息框并在需要时进行保存。 在最坏的情况下,只要我能理解,你的列表将只包含主表单。

相关问题