0

我有一个Windows Form使用MDI. 我有一个方法负责保存任何打开的可编辑表单中的数据,并且为不同的事件调用此方法。但我也在父表单before close事件中使用它,我需要检查所有打开的 MDIchilds,如果其中有可编辑的表单,如果有,请求保存。除此之外,我只关心它ActiveMdiChild是否可编辑并且只要求保存它。

这是完成这项工作的方法:

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是每个人都继承甚至父窗体的窗体。现在我已经完成了将代码放在一个方法中,所以现在我只调用这个方法,但困扰我的是这两个部分几乎相同,但我仍然不知道如何优化逻辑。

4

3 回答 3

3

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

方案:

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, ....

这是有效的,因为&&(like ||) 是短路运算符,这意味着一旦条件被证伪,从左到右,其余的操作数都不会被计算。

于 2013-02-07T09:45:27.657 回答
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();
        }       
    }
}
于 2013-02-07T09:50:45.507 回答
0

您可以将有效表单收集到 List 中,其中 T 可以是实现业务逻辑所需的所有内容的接口(也许 List 本身就足够了)。元素(表单)应添加到此列表中,然后在随后的循环中您可以遍历此列表,显示消息框并在需要时保存。据我所知,在最坏的情况下,您的列表将只包含主要形式。

于 2013-02-07T09:51:26.507 回答