如何知道当一个用户控件不再使用(没有关闭的事件) [英] How to know when a UserControl is no longer used (no Close event)

查看:727
本文介绍了如何知道当一个用户控件不再使用(没有关闭的事件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用用户控件 s的的WinForm 里面显示的数据。 我还使用了Command模式,使用户控件注册一个(弱)事件,并做东西,当命令被触发。

I'm using UserControls inside a WinForm to display data. I'm also using the command pattern so that the UserControl registers a (weak) event and does stuff when the command is triggered.

在一个的WinForm 我可以注销在关闭事件的事件。但是有没有这样的事件在用户控件。 我迷上了我以为事件将被解雇时,用户控件不再显示堆栈,而且能找到什么有用的。 为了获得由我检查为空,并且工作了大多数情况下。

On a WinForm I can unregister the events in the Close event. However there's no such event on a UserControl. I hooked up the events I thought would be fired when the UserControl is no longer in the display stack but could find nothing useful. To get by I check if the Parent is null and that worked for most of the cases.

现在我想有一个孩子用户控件的另一个用户控件(把用户控件的TabControl )和属性不会是空对了时,不再显示父子控件。

Now I'd like to have a child UserControl of another UserControl (put the UserControl inside a TabControl) and the Parent property isn't going to be null for the child control anymore when the parent is no longer displayed.

有没有办法知道,如果用户控件用?

Is there any way to know if the UserControl is used?

我已经试过到目前为止:的Dispose()不会被调用马上被系统所以它是没有有用的; 可见性没有得到由系统更新要么;没有关闭卸载触发的事件。

What I've tried so far: Dispose() doesn't get called straight away by the system so it's no useful; IsVisible doesn't get updated by the system either; there is no Close or Unload event fired.

干杯。

推荐答案

这真的是Dispose()方法。如果它不被调用及早再有就是在使用该控件的code的错误。使用Controls.Clear()或Controls.Remove()为例。

It really is the Dispose() method. If it doesn't get called early enough then there's a bug in the code that uses the control. Using Controls.Clear() or Controls.Remove() for example.

控件的父母总是遍历其控制的收集和处置的子控件时,它得到处理。这使得自动配置,开始在窗体的Dispose()方法,当窗体关闭运行。然而的没有的自动,当你删除自己控制。

The parent of a control always iterates its Controls collection and disposes the child controls when it gets disposed. Which makes disposing automatic, starting at the form's Dispose() which runs when the form is closed. It is however not automatic when you remove controls yourself.

这篇关于如何知道当一个用户控件不再使用(没有关闭的事件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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