Windows Forms ToolTip will not re-appear after fir

2019-01-11 22:36发布

I have a Windows Forms C# application where I would like to use a tooltip on one of the text boxes. I initialize the tool-tip in the constructor of the Form class, and it works the first time. So when I hover over the text box with my mouse it works, but once the toolTip times out and it goes away, it does not re-appear when I move my mouse away and back onto the control. I would expect it to come back. What am I doing wrong?

Here is how I initialize the tooltip:

myTip = new ToolTip();
myTip.ToolTipIcon = ToolTipIcon.Info;
myTip.IsBalloon = true;
myTip.ShowAlways = true;

myTip.SetToolTip(txtMyTextBox,"My Tooltip Text");

9条回答
走好不送
2楼-- · 2019-01-11 22:42

I had a similar problem today. Sometimes, the tooltip would not show. I had one ToolTip control for all the controls in my form.

I also had a MouseEnter event on all the controls added automatically, so I modified the MouseEnter event to do:

_tooltip.Active = false;
_tooltip.Active = true;

It fixed the bug, but I don't know why.

Also, the bug always happened on Windows XP machines, but not on Windows Vista.

查看更多
太酷不给撩
3楼-- · 2019-01-11 22:43

I solved this problem by this

if (t == null)
{
    t = new ToolTip();
}
t.IsBalloon = true;
t.ToolTipTitle = "Stop";
t.ToolTipIcon = ToolTipIcon.Error;
t.Show("", yourControlonWhichToApplyToolTip, 0);

t.Show("PDescription", yourControlonWhichToApplyToolTip, 1000);

Note i have added an empty tooltip.

查看更多
够拽才男人
4楼-- · 2019-01-11 22:53

For what it's worth, I was having this problem on my Windows XP system until I noticed that if I placed at least one tooltip control on my form manually (from the toolbox) I could create as many tooltips as needed within my code, and they would all work.

If, however, I tried to create all tooltips in code (say for instance in the formload event) the tips would only show once and never to be seen again. I can't give you the exact "why this happens" story, but I have duplicated this issue several times always with the same effect. It might have something to do with the object scope, but I'm not sure.

So now just as a habit, I always include at least one Visual Studio tooltip control and then the rest within my code.

查看更多
地球回转人心会变
5楼-- · 2019-01-11 22:55

I had a similar problem today. VS 2010 SP1 .Net 3.5
After AutoPopDelay-Time the ToolTip do not show the Controls ToolTipText.
Kevins solution is the only way to solve the problem.

I encapsulate this in my own ToolTip class:

public class ToolTip : System.Windows.Forms.ToolTip 
{
    public ToolTip() : base() { }

    public ToolTip(System.ComponentModel.IContainer components) : base(components) { }

    public new void SetToolTip(System.Windows.Forms.Control ctl, string caption) 
    {
        ctl.MouseEnter -= new System.EventHandler(toolTip_MouseEnter);
        base.SetToolTip(ctl, caption);
        if(caption != string.Empty)
        ctl.MouseEnter += new System.EventHandler(toolTip_MouseEnter);
    }

    private void toolTip_MouseEnter(object sender, EventArgs e) 
    {
        this.Active = false;
        this.Active = true;
    }
}
查看更多
Ridiculous、
6楼-- · 2019-01-11 22:56

In my case after setting the tooltip text with the SetToolTip method, I used the Show overload with duration parameter, i.e.

toolTip.Show(text, textEdit, 1000);

After that tooltip did not reappear on mouse hover, and resetting tooltip.Active didn't work..

A workaround that worked for me was to use Show overload without the duration, and hide it manually afterwards:

toolTip.Show(text, textEdit);
new Task(() =>
{
    Thread.Sleep(750);
    textEdit.Invoke(new Action(() => toolTip.Hide(textEdit)));
}).Start();

With this code I have the desired behaviour, i.e.

  1. The tooltip is shown at once for 750 millisec. after the tooltip text has changed
  2. The tooltip does appear for the specified time when the mouse is over the control
查看更多
再贱就再见
7楼-- · 2019-01-11 23:02

I had this issue in VB.NET. What I did was drop a TooTip control on the form, and then on the target control's MouseHover event, I set the properties of the ToolTip. I did this because I used one ToolTip control for five different Label controls. It worked great. (Really, I wanted the ToolTip to show immediately, so I used the MouseEnter event instead.) I can post my exact code tomorrow when I get to work.

查看更多
登录 后发表回答