.Net 中的 Mdi 儿童发生了变化

发布于 2024-08-29 08:25:43 字数 201 浏览 2 评论 0原文

是否有一种简单的方法来跟踪 Mdi 子项的更改,即当它们被创建和关闭时,例如 OnMdiChildListChanged 事件(我意识到这实际上并不存在)。

我还知道我可以在 Mdi 中有一个方法来处理子表单的创建并记录它们的状态,甚至创建一个接口来定义子表单具有“NotifyParent”方法,然后在关闭时调用该方法表格,但我想知道是否有任何我可以深入研究的内置事件?

Is there a simple way of tracking the change of an Mdi's children i.e. when they are created and closed, something like an event OnMdiChildListChanged (I realise this doesn't actually exist).

I am also aware that I could have a method within my Mdi that handles the creation of child forms and logs the state of them or even create an Interface that defines that a child form has a "NotifyParent" method that is then called on close of the form, but i was wondering if there was any built in events that i could plumb into?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天赋异禀 2024-09-05 08:25:43

该管道已经存在,它用于自动更新 MDI 窗口列表(MenuStrip.MdiWindowListItem 属性)。 Form 类在 OnVisibleChanged 和 OnMdiChildActivate 方法中包含用于更新菜单的代码。不过,希望是空的,您看不到菜单项列表的更改,也无法覆盖任何管道代码。并非没有在您自己的子表单中执行类似操作并重载这些方法。

为了解决这个问题,只需在 MDI 父窗体中编写一个公共方法,您始终使用该方法来添加新的子窗口:

public void AddChild(Form child) {
  child.MdiParent = this;
  child.FormClosed += child_FormClosed;
  // Run your code to handle new child windows here...
}
private void child_FormClosed(object sender, FormClosedEventArgs e) {
  // Your code to handle closed child windows here...
}

您甚至可以将 AddChild 设为静态,因为只有一个 MDI 父窗体。

This plumbing already exists, it is used to automatically update the MDI window list (MenuStrip.MdiWindowListItem property). The Form class has code in the OnVisibleChanged and OnMdiChildActivate methods to update the menu. Idle hope though, You can't see the menu item list change, nor can you override any of the plumbing code. Not without doing similar and overloading these methods in your own child forms.

Punt this problem, simply write a public method in your MDI parent form that you consistently use to add new child windows:

public void AddChild(Form child) {
  child.MdiParent = this;
  child.FormClosed += child_FormClosed;
  // Run your code to handle new child windows here...
}
private void child_FormClosed(object sender, FormClosedEventArgs e) {
  // Your code to handle closed child windows here...
}

You can even make AddChild static, since there's only ever one MDI parent.

嗫嚅 2024-09-05 08:25:43

当 Mdi 子项打开或关闭时,MdiChildActivate(或 OnMdiChildActivate)将触发。

MdiChildActivate (or OnMdiChildActivate) will fire when a Mdi child is opened or closed.

柠檬 2024-09-05 08:25:43

您可以创建自己的事件,并自己在关联的方法中触发它。它非常简单,您可以像订阅内置事件一样订阅它。

You could create your own event, and fire it in the associated methods yourself. It's fairly simple, and you can subscribe to it just like a built in event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文