关于PropertyStore和MDI子窗体 [英] About PropertyStore and MDI child form

查看:191
本文介绍了关于PropertyStore和MDI子窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

该子窗体调用示例code:

  //将code为主要形式。
变种F =新FormMDIChild();
f.MdiParent =这一点;
f.Show();
 

解决方案

有关的记录,在引用后所提供的解决方案不工作(虽然这是一个有点冒险)。然而,泄漏也消失,如果你打开​​和关闭另一个孩子的形式,看来,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 });
}

这篇关于关于PropertyStore和MDI子窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆