关于PropertyStore和MDI子窗体窗体、PropertyStore、MDI

由网友(我没抽风,是风在抽我)分享简介:这是关于MDI的设置.NET WinForm的问题。在主窗体创建MDI子窗体,主窗体的 PropertyStore 持有引用到MDI子窗体。我不知道这是否会造成孩子的形式是活的,即使它已关闭。如果是这样,我该怎么做的时候,以消除该参考配置的子窗体?该子窗体调用示例code://将code为主要形式。变种F =新For...

这是关于MDI的设置.NET WinForm的问题。

在主窗体创建MDI子窗体,主窗体的 PropertyStore 持有引用到MDI子窗体。我不知道这是否会造成孩子的形式是活的,即使它已关闭。如果是这样,我该怎么做的时候,以消除该参考配置的子窗体?

该子窗体调用示例code:

  //将code为主要形式。
变种F =新FormMDIChild();
f.MdiParent =这一点;
f.Show();
 
vb中mdi窗体中子窗体的大小

解决方案

有关的记录,在引用后所提供的解决方案不工作(虽然这是一个有点冒险)。然而,泄漏也消失,如果你打开​​和关闭另一个孩子的形式,看来,MDI父只泄漏最后打开的孩子。

如果你想用周围提到的引用岗位的工作,以修复泄漏,只是重写的MdiParent的OnMdiChildActivate方法...

 保护覆盖无效OnMdiChildActivate(EventArgs的发送)
{
    base.OnMdiChildActivate(E);

    typeof运算(表格).InvokeMember(FormerlyActiveMdiChild
        BindingFlags.Instance | BindingFlags.SetProperty |
        BindingFlags.NonPublic可,空,为此,新的对象[] {空});
}
 

This is .net WinForm question about MDI setting.

When the main form creates an MDI child form, the main form's PropertyStore holds a reference to the MDI child form. I wonder whether this will cause the child form to be alive even if it is closed. If so, what shall I do when disposing the child form in order to remove this reference?

The child form is called by sample code:

//The code is in the main form.
var f = new FormMDIChild();
f.MdiParent = this;
f.Show();

解决方案

For the record, the solution offered in the referenced post does work (though it's a little dicey). However, the leak also goes away if you open and close another child form, it appears that the MDI Parent only leaks the last opened child.

If you want to fix the leak by using the work around mentioned in the referenced post, just override the MDIParent's OnMdiChildActivate method...

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

    typeof(Form).InvokeMember("FormerlyActiveMdiChild", 
        BindingFlags.Instance | BindingFlags.SetProperty |
        BindingFlags.NonPublic, null, this, new object[] { null });
}

阅读全文

相关推荐

最新文章