开发者

Mousehover, tooltip showing multiple times

I have a custom control (C#, visual studio). I want to show a tooltip on the mousehover event.

However, no matter what I do, it either never shows or has a chance of showing multiple times.

I thought it would be as simple as开发者_如何学JAVA:

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

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

But this does not work. I have tried a bunch of things but cannot seem to get it to work. I would like to have the tooltip be part of the component because I want to access private fields from it for display.

Thanks for any help


Have you tried instantiating the tooltip in your constructor and showing it on the mouse hover?

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


The MouseHover is fired every time the mouse moves over your control. So your are creating a new tooltip every single time the event is fired. That's why you see multiple instances of this widget. Try the Joseph's answer


Just adding a tooltip using the designer generates wildly different code than that in the question.

Form1.Designer.cs: (private variables moved to the top of the class for readability)

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

        // ...
    }
}

I'm sure you could extract just the tooltip and container stuff into your component.


Read MSDN it's all there!

You can try another solution:


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;
}

Hope i help

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜