简体   繁体   English

关闭表单后更新MDI子表单列表

[英]Updating MDI child forms list after closing a form

I am using DevExpress NavBar as main menu for my MDI application, and one of NavBar's groups contains items that represent opened MDI child forms. 我将DevExpress NavBar用作我的MDI应用程序的主菜单,并且NavBar的组之一包含代表打开的MDI子窗体的项目。 I am having trouble with updating a menu when a MDI child form closes. MDI子窗体关闭时,我无法更新菜单。

I have to use Form.MdiChildren collection to generate menu group, but the problem is, when using Form.FormClosing event, that closed form is still in Form.MdiChildren collection. 我必须使用Form.MdiChildren集合来生成菜单组,但是问题是,当使用Form.FormClosing事件时,该关闭的窗体仍在Form.MdiChildren集合中。 I tried to use a System.Timers.Timer to wait 1 second and then update a menu, but I get various exceptions because of asynchronous behavior (when a user closes few forms very fast). 我尝试使用System.Timers.Timer等待1秒钟,然后更新菜单,但是由于异步行为(当用户非常快地关闭一些表格时),我会遇到各种异常。

I also cannot maintain my own list of MDI children, because of complexity of classes design. 由于类设计的复杂性,我也无法维护自己的MDI子级列表。

Does anyone have some elegant solution for this? 有人对此有一些优雅的解决方案吗?

I have had success with using this combination of methods: 使用这种方法的组合,我已经取得了成功:

private List<Form> _childForms = new List<Form>();

protected override void OnMdiChildActivate(EventArgs e)
{
   base.OnMdiChildActivate(e);

   Form form = ActiveMdiChild;
   if (form == null)
       return;
   else
   {
       if (!_childForms.Contains(form))
       {
           _childForms.Add(form);
           form.FormClosed += mdiChildForm_FormClosed;
       }
   }
}

private void mdiChildForm_FormClosed(Object sender, FormClosedEventArgs e)
{
   var form = (Form)sender;
   if (_childForms.Contains(form))
       _childForms.Remove(form);
   if (_childForms.Count > 0)
       _childForms[_childForms.Count - 1].Activate();
}

Note that the Activate method is called pretty much anytime the user interacts with a child form. 请注意,几乎在用户与子窗体进行交互时,几乎都会调用Activate方法。 That includes opening and closing them. 这包括打开和关闭它们。

You can then make use of the childForms collection to always know the open forms and do what you like with them. 然后,您可以使用childForms集合来始终了解打开的表单并对其进行所需的操作。

"I also cannot maintain my own list of MDI children, because of complexity of classes design." “由于类设计的复杂性,我也无法维护自己的MDI子级列表。”

Is this because of the different class types? 这是因为类类型不同吗? What about holding a list of base classes? 拿着基类列表怎么办? like: List<Form> When there is a FormClosed event, just remove that form from the list. 像: List<Form>发生FormClosed事件时,只需从列表中删除该表单即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM