关于 PropertyStore 和 MDI 子窗体

编程入门 行业动态 更新时间:2024-10-26 20:25:47
本文介绍了关于 PropertyStore 和 MDI 子窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is WinForm question about MDI setting.

当主窗体创建 MDI 子窗体时,主窗体的 PropertyStore 保存对 MDI 子窗体的引用.我想知道这是否会导致子窗体即使关闭也能存活.如果是这样,在处理子表单时我该怎么做才能删除此引用?

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();

推荐答案

作为记录,引用的帖子中提供的解决方案确实有效(尽管有点冒险).但是,如果您打开和关闭另一个子窗体,泄漏也会消失,看来 MDI 父级只会泄漏最后一个打开的子窗体.

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.

如果您想使用参考帖子中提到的解决方法来修复泄漏,只需覆盖 MDIParent 的 OnMdiChildActivate 方法...

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屋!

更多推荐

[db:关键词]

本文发布于:2023-03-29 00:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/744737.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:窗体   PropertyStore   MDI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!