鼠标悬停,工具提示多次显示

时间:2009-11-03 16:19:06

标签: c# tooltip mousehover

我有一个自定义控件(C#,visual studio)。我想在mousehover事件上显示工具提示。

然而,无论我做什么,它都不会显示或有机会多次显示。

我认为这很简单:

private void MyControl_MouseHover(object sender, EventArgs e)
{
    ToolTip tT = new ToolTip();

    tT.Show("Why So Many Times?", this);
}

但这不起作用。我尝试了很多东西,但似乎无法让它发挥作用。我想让工具提示成为组件的一部分,因为我想从中访问私有字段以供显示。

感谢您的帮助

4 个答案:

答案 0 :(得分:9)

您是否尝试在构造函数中实例化工具提示并在鼠标悬停时显示它?

public ToolTip tT { get; set; }

public ClassConstructor()
{
    tT = new ToolTip();
}

private void MyControl_MouseHover(object sender, EventArgs e)
{
    tT.Show("Why So Many Times?", this);
}

答案 1 :(得分:1)

每次鼠标移动到控件上时,都会触发MouseHover。因此,每次触发事件时,您都在创建一个新的工具提示。这就是你看到这个小部件的多个实例的原因。试试约瑟夫的回答

答案 2 :(得分:1)

使用设计器添加工具提示会产生与问题中不同的代码。

Form1.Designer.cs :(私有变量移到了类的顶部以便于阅读)

partial class Form1
{
    private System.ComponentModel.IContainer components = null;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.ToolTip toolTip1;

    // ...

    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.label1 = new System.Windows.Forms.Label();
        this.toolTip1 = new System.Windows.Forms.Tooltip(this.components);

        // ...

        this.toolTip1.SetToolTip(this.label1, "abc");

        // ...
    }
}

我确信您只需将工具提示和容器内容提取到组件中即可。

答案 3 :(得分:0)

阅读MSDN就在那里!

您可以尝试其他解决方案:


private System.Windows.Forms.ToolTip toolTip1;

private void YourControl_MouseHover(object sender, EventArgs e)
{
     toolTip1 = new System.Windows.Forms.ToolTip();
     this.toolTip1.SetToolTip(this.YourControl, "Your text here :) ");
     this.toolTip1.ShowAlways = true;
}

希望我帮助