C# 首次使用后,Windows 窗体工具提示不会重新出现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/559707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows Forms ToolTip will not re-appear after first use
提问by 7wp
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?
我有一个 Windows 窗体 C# 应用程序,我想在其中一个文本框上使用工具提示。我在 Form 类的构造函数中初始化工具提示,它第一次工作。因此,当我用鼠标将鼠标悬停在文本框上时,它可以工作,但是一旦工具提示超时并消失,当我将鼠标移开并移回控件时它不会重新出现。我希望它回来。我究竟做错了什么?
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");
采纳答案by Kevin Doyon
I had a similar problem today. Sometimes, the tooltip would not show. I had one ToolTip control for all the controls in my form.
我今天遇到了类似的问题。有时,工具提示不会显示。我的表单中的所有控件都有一个 ToolTip 控件。
I also had a MouseEnter event on all the controls added automatically, so I modified the MouseEnter event to do:
我在所有自动添加的控件上也有一个 MouseEnter 事件,所以我修改了 MouseEnter 事件:
_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.
此外,该错误总是发生在 Windows XP 机器上,但不会发生在 Windows Vista 上。
回答by HardCode
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.
我在 VB.NET 中遇到过这个问题。我所做的是在窗体上放置一个TooTip 控件,然后在目标控件的MouseHover 事件上设置ToolTip 的属性。我这样做是因为我将一个 ToolTip 控件用于五个不同的 Label 控件。它工作得很好。(实际上,我希望 ToolTip 立即显示,所以我改用 MouseEnter 事件。)我明天上班时可以发布我的确切代码。
回答by Silverwing
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.
对于它的价值,我在我的 Windows XP 系统上遇到了这个问题,直到我注意到如果我手动(从工具箱)在我的表单上放置至少一个工具提示控件,我可以在我的代码中根据需要创建尽可能多的工具提示,并且它们都会工作。
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.
但是,如果我尝试在代码中创建所有工具提示(例如在 formload 事件中),这些提示将只显示一次并且再也不会出现。我不能给你确切的“为什么会发生这种情况”的故事,但我已经多次重复这个问题,效果总是一样的。它可能与对象范围有关,但我不确定。
So now just as a habit, I always include at least one Visual Studio tooltip control and then the rest within my code.
所以现在作为一种习惯,我总是至少包含一个 Visual Studio 工具提示控件,然后在我的代码中包含其余的控件。
回答by user276648
I guess you'll be happy to know that Microsoft knows about it...since about 5 years...
我想你会很高兴知道微软知道这件事......大约 5 年以来......
- 2/21/2005 Bug acknowledged as reproducable
- 3/29/2005 Hum we might fix it, but later...
- 11/15/2005 Well actually it's not a big bug, and it doesn't happen much, so we won't fix it.
- 2/21/2005 错误被确认为可重现
- 2005 年 3 月 29 日 嗯,我们可能会修复它,但稍后...
- 2005 年 11 月 15 日 嗯,实际上这不是一个大错误,而且发生的次数也不多,所以我们不会修复它。
Damn I love it when I stumble on bugs Microsoft doesn't want to solve! This time it's called a corner case, last time it was simply too difficult to resolve...
该死的,当我偶然发现 Microsoft 不想解决的错误时,我喜欢它!这一次被称为角落案件,上次实在是太难解决了……
I'm off to tell my client that the bugs in my program are just corner cases and too difficult to resolve...
我要告诉我的客户,我的程序中的错误只是极端情况,很难解决......
回答by Zain Ali
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.
请注意,我添加了一个空的工具提示。
回答by j.la
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.
我今天遇到了类似的问题。VS 2010 SP1 .Net 3.5
AutoPopDelay-Time 后工具提示不显示控件ToolTipText
。
凯文的解决方案是解决问题的唯一方法。
I encapsulate this in my own ToolTip class:
我将其封装在我自己的 ToolTip 类中:
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;
}
}
回答by Gabe
I just had the the problem on Windows 7 so I found this thread.
我刚刚在 Windows 7 上遇到了这个问题,所以我找到了这个线程。
In my case this did not work in tooltip_MouseEnter:
在我的情况下,这在 tooltip_MouseEnter 中不起作用:
tooltip.Active = false;
tooltip.Active = true;
So I tried the following:
所以我尝试了以下方法:
this.toolTip.SetToolTip(this.txtbx1, "tooltip-text");
This worked fine for me.
这对我来说很好。
回答by horgh
In my case after setting the tooltip text with the SetToolTip
method, I used the Show
overload with duration
parameter, i.e.
在我的情况下,在使用SetToolTip
方法设置工具提示文本后,我使用了Show
带duration
参数的重载,即
toolTip.Show(text, textEdit, 1000);
After that tooltip did not reappear on mouse hover, and resetting tooltip.Active
didn't work..
在鼠标悬停时该工具提示没有重新出现之后,并且重置tooltip.Active
不起作用..
A workaround that worked for me was to use Show
overload without the duration, and hide it manually afterwards:
对我有用的解决方法是使用Show
没有持续时间的重载,然后手动隐藏它:
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.
使用此代码,我具有所需的行为,即
- The tooltip is shown at once for 750 millisec. after the tooltip text has changed
- The tooltip does appear for the specified time when the mouse is over the control
- 工具提示一次显示 750 毫秒。工具提示文本更改后
- 当鼠标悬停在控件上时,工具提示会出现指定的时间
回答by Tenesi
System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
private void textBox_MouseHover(object sender, EventArgs e)
{
ToolTip1.Show("YOUR TEXT", textBox);
}
private void textBox_MouseLeave(object sender, EventArgs e)
{
ToolTip1.Active = false;
ToolTip1.Active = true;
}