C# 如何在UserControl中自动清理工具提示

C# 如何在UserControl中自动清理工具提示,c#,.net,winforms,.net-4.0,user-controls,C#,.net,Winforms,.net 4.0,User Controls,我一直在追踪一个内存泄漏,并将其缩小为在UserControl派生类中分配的工具提示 工具提示在控件的构造函数中分配,并在加载事件中初始化,如下所示: public class CommonProfile : System.Windows.Forms.UserControl { private ToolTip toolTip1; ... public CommonProfile() { InitializeComponent();

我一直在追踪一个内存泄漏,并将其缩小为在UserControl派生类中分配的工具提示

工具提示在控件的构造函数中分配,并在加载事件中初始化,如下所示:

public class CommonProfile : System.Windows.Forms.UserControl
{
    private ToolTip toolTip1;

    ...

    public CommonProfile()
    {
        InitializeComponent();

        // Create the ToolTip and associate with the Form container.
        toolTip1 = new ToolTip(this.components);
    }

    private void CommonProfile_Load(object sender, System.EventArgs e)
    {
        // Set up the delays for the ToolTip.
        toolTip1.AutoPopDelay = 5000;
        toolTip1.InitialDelay = 1000;
        toolTip1.ReshowDelay = 500;
        // Force the ToolTip text to be displayed whether or not the form is active.
        toolTip1.ShowAlways = true;

        // Set up the ToolTip text
        toolTip1.SetToolTip(this.btnDeleteEntry, "Delete this Profile");
        toolTip1.SetToolTip(this.lblProfileType, "Edit this Profile");
        toolTip1.SetToolTip(this.lblProfileData, "Edit this Profile");
        toolTip1.SetToolTip(this.picFlagForUpdate, "Toggle Flag for Update");
    }    
}
public void Shutdown()
{
    toolTip1.RemoveAll();
}
控件的父控件的生存期超过了控件的生存期。此控件将动态创建并添加到面板控件,然后从面板控件中删除

我发现控件的Dispose成员没有被调用,显然是因为对工具提示的引用仍然存在

我添加了如下关闭方法:

public class CommonProfile : System.Windows.Forms.UserControl
{
    private ToolTip toolTip1;

    ...

    public CommonProfile()
    {
        InitializeComponent();

        // Create the ToolTip and associate with the Form container.
        toolTip1 = new ToolTip(this.components);
    }

    private void CommonProfile_Load(object sender, System.EventArgs e)
    {
        // Set up the delays for the ToolTip.
        toolTip1.AutoPopDelay = 5000;
        toolTip1.InitialDelay = 1000;
        toolTip1.ReshowDelay = 500;
        // Force the ToolTip text to be displayed whether or not the form is active.
        toolTip1.ShowAlways = true;

        // Set up the ToolTip text
        toolTip1.SetToolTip(this.btnDeleteEntry, "Delete this Profile");
        toolTip1.SetToolTip(this.lblProfileType, "Edit this Profile");
        toolTip1.SetToolTip(this.lblProfileData, "Edit this Profile");
        toolTip1.SetToolTip(this.picFlagForUpdate, "Toggle Flag for Update");
    }    
}
public void Shutdown()
{
    toolTip1.RemoveAll();
}
调用Shutdown方法可以消除泄漏,并最终调用Dispose

不幸的是,该解决方案要求使用该控件的人在完成该控件时记住调用Shutdown方法


我想知道是否有某种方法可以自动执行此操作,从而无需显式调用关机方法。

您没有从
面板中显示如何处理
用户控件的代码

只是打电话:

panel1.Controls.Remove(userControl1);
不会处理
UserControl

您需要特别致电:

userControl1.Dispose();
这也会自动将其从
面板中删除。在您的
UserControl
中,如果您需要自己进行清理,请尝试订阅它自己的Dispose事件:

private ToolTip toolTip1;

public UserControl1() {
  InitializeComponent();
  // tooltip initialization
  this.Disposed += UserControl1_Disposed;
}

private void UserControl1_Disposed(object sender, EventArgs e) {
  if (toolTip1 != null)
    toolTip1.Dispose();
}

在控件dispose方法中,还需要在工具提示中显式调用dispose

我想您需要在工具提示中调用dispose,或者在dispose-of-usercontrol中调用dispose,或者您可以使用一个清理函数来完成此操作,比如关闭方法。1。看这里[1]: