C# 我可以为 .NET Windows 窗体窗口中的工具提示设置无限的 AutoPopDelay 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/805430/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 01:54:09  来源:igfitidea点击:

Can I set an infinite AutoPopDelay for a tooltip in a .NET Windows Forms window?

c#.netwinformstooltip

提问by Handleman

I have a requirement to not have the standard .NET Windows Formstooltip automatcially hide - that is, I need them to remain visible until the mouse moves off the control that has the tooltip. I'd like to avoid having to use specific MouseEnter and MouseLeave events for all controls with a tooltip. I'm happy to hear about third-party solutions if they'd solve the problem.

我有一个要求,不要让标准的 .NET Windows 窗体工具提示自动隐藏 - 也就是说,我需要它们保持可见,直到鼠标移出具有工具提示的控件。我想避免对带有工具提示的所有控件使用特定的 MouseEnter 和 MouseLeave 事件。如果第三方解决方案可以解决问题,我很高兴听到他们的消息。

回答by CSharper

Set the ToolTipService.ShowDuration attached property to int.MaxValue:

将 ToolTipService.ShowDuration 附加属性设置为 int.MaxValue:

<Button 
  Height="23" Width="75"
  Content="Click Me" 
  ToolTip="My ToolTip" ToolTipService.ShowDuration="2147483647" 
/>

回答by Tim Robinson

The ToolTip.StopTimermethod might help. From MSDN:

ToolTip.StopTimer方法可能会有所帮助。来自 MSDN:

The ToolTip class has an internal timer that it uses to set the display duration for ToolTips. The duration associated with this timer is set through the AutoPopDelay property. The StopTimer method will stop this internal timer, causing any displayed ToolTip to be shown modally until the Hide method is called, or the parent form is minimized, hidden, or closed.

ToolTip 类有一个内部计时器,用于设置 ToolTips 的显示持续时间。与此计时器关联的持续时间是通过 AutoPopDelay 属性设置的。StopTimer 方法将停止这个内部计时器,导致任何显示的工具提示以模态方式显示,直到调用 Hide 方法,或者父窗体被最小化、隐藏或关闭。

回答by Pieter Breed

This is not going to be the answer you want to hear... Roll your own. I once had scenario where I wanted to simulate the new Office's ribbonbar tooltip behavour for shortcut keyboard commands and I found that the standard tooltip API is just too limited.

这不会是你想听到的答案......自己动手。我曾经有一个场景,我想为快捷键键盘命​​令模拟新的 Office 功能区工具提示行为,但我发现标准工具提示 API 太有限了。

Make a small, simple and border-less form instance for every control that you need a tooltip for. Then show and hide them non-modally as you do a mouse-enter or -leave event. Have the constructor for this window receive the UI control instance and let it query the control for it's events so that it can attach itself to the mouse-enter and -leave events, so that you wouldn't have to wire them up manually.

为您需要工具提示的每个控件制作一个小型、简单且无边框的表单实例。然后在执行鼠标输入或离开事件时以非模态方式显示和隐藏它们。让这个窗口的构造函数接收 UI 控件实例,让它查询控件的事件,以便它可以将自己附加到鼠标输入和离开事件,这样您就不必手动连接它们。

回答by Handleman

So, I ended up de-activating the tooltip, but I am still using it to hold the text for the tooltip, because it neatly adds itself to all the controls. I made my own panel and label inside it for the tooltip and dynamically positioned, made visible and populated the text on the mouse enter and mouse leave events of all the controls.

所以,我最终停用了工具提示,但我仍然使用它来保存工具提示的文本,因为它巧妙地将自己添加到所有控件中。我为工具提示制作了自己的面板和标签,并在其中动态定位、显示并填充了所有控件的鼠标进入和鼠标离开事件上的文本。

I also wrote a recursive function to add the mouse events to each label control (the only ones I have tooltips on), so I didn't have to manually to it and won't have to remember to do it when I add a new label. New developers on the project may not ever realise that the tooltip control is not actually rendering the tips.

我还编写了一个递归函数来将鼠标事件添加到每个标签控件(我唯一有工具提示的控件),因此我不必手动对其进行操作,并且在添加新控件时不必记住这样做标签。该项目的新开发人员可能永远不会意识到工具提示控件实际上并未呈现提示。

Why a label in a panel? - so really big tooltips can scroll - although this is still not working nicely.

为什么要在面板上贴标签?- 所以真的很大的工具提示可以滚动 - 尽管这仍然不能很好地工作。

It is not real pretty, and I am still looking for a good answer if you've got on (or a critique of the method I've employed).

这不是真的很漂亮,如果你已经开始(或对我采用的方法的批评),我仍在寻找一个好的答案。

I came across http://www.binarymission.co.uk/, but their tooltip did not seem to render properly in my .NET 3.5 application. I am still talking to them.

我遇到了http://www.binarymission.co.uk/,但他们的工具提示在我的 .NET 3.5 应用程序中似乎没有正确呈现。我还在和他们说话。

回答by serhio

Try myToolTip.ReshowDelay = 0.

试试myToolTip.ReshowDelay = 0

You just reshow the tooltip immediately after its delay expires. It worked for me.

您只需在延迟到期后立即重新显示工具提示。它对我有用。

回答by panny

I was searching for a solution to the problem

我正在寻找问题的解决方案

  1. The popup baloon disappears too fast, not allowing me to read its content
  2. The popup baloon does not appear again when hovering into that region.
  1. 弹出气球消失得太快,我无法阅读其内容
  2. 悬停在该区域时,弹出气球不会再次出现。

I found in the forums the following answer, which does not solve Problem2, but lets me read the text for more than the time I need when I set its value to 32766and is thus - in my opinion - a kind of 'work around':

我在论坛下面的答案,这并不解决Problem2发现,但让我阅读的文本比我其值设置为我所需要的时间更多的32766,因此-在我看来-一种“变通” :

Actually, I found out my problem was that I assigned a value higher than 32767 (32768 and higher will have an effect of 5 sec) really weird, since AutoPopDelay is supposed to take an Integer (32 bit) as parameter, and 32767 is really a signed 16 bit ...

实际上,我发现我的问题是我分配的值高于 32767(32768 和更高将产生 5 秒的效果)真的很奇怪,因为 AutoPopDelay 应该采用整数(32 位)作为参数,而 32767 确实是一个有符号的 16 位 ...

But I have to admit, it still sucks...

但我不得不承认,它仍然很糟糕......

But setting it to 32000(32 seconds) seems to work better. No clue why this is but maybe it helps someone.

但是将其设置为32000(32 秒)似乎效果更好。不知道为什么会这样,但也许它可以帮助某人。

回答by Peter Perhá?

I think you are trying to cure the symptoms and not the cause of your problem.

我认为您正在尝试治愈症状而不是问题的原因。

You are trying to force a ToolTip to be something different from a ToolTip. A tooltip is, by definition, "a small "hover box" with information about the item being hovered over".

您试图强制工具提示与工具提示不同。根据定义,工具提示是“一个小的“悬停框”,其中包含有关悬停在其上的项目的信息”。

If the user can't read what the tooltip is trying to suggest in 30 seconds, then it isn't really a "tip" but an entire chapter from the help file. Put lengthy instructions into the Manual and do not attempt to force it into a ToolTip that stays open forever.

如果用户无法在 30 秒内阅读工具提示试图建议的内容,那么它就不是真正的“提示”,而是帮助文件中的一整章。将冗长的说明放入手册中,不要试图强行将其放入永远保持打开状态的工具提示中。

回答by Gullbyrd

In my limited experience, I have found that setting the showAlways property to true has the side effect of making the tip display "infinitely" (until the cursor exits the control). This is not what the documentation says it will do, but that's what I have experienced. (However, I am using the Janus Supertip component, and it's possible that affects the behavior).

在我有限的经验中,我发现将 showAlways 属性设置为 true 具有使提示“无限”显示的副作用(直到光标退出控件)。这不是文档所说的它会做的,但这就是我所经历的。(但是,我使用的是 Janus Supertip 组件,它可能会影响行为)。

回答by Bert Regelink

In my application, the tooltip text has to update every 10 seconds. What works for me is to deactivate and activate the tooltip every time this text is updated. When I set the AutoPopDelay to more than 10 seconds, the tooltip will effectively stay forever. So you might use a timer to repeatedly activate and deactivate the tooltip:

在我的应用程序中,工具提示文本必须每 10 秒更新一次。对我有用的是每次更新此文本时停用和激活工具提示。当我将 AutoPopDelay 设置为超过 10 秒时,工具提示将有效地永久保留。因此,您可以使用计时器重复激活和停用工具提示:

private void timer1_Tick(object sender, EventArgs e)
{
    toolTip1.Active = false;
    toolTip1.Active = true;
}

To minimize the flicker that happens on reactivating the tooltip, set the InitialDelay to 1. When you set the AutoPopDelay to 30000 and the timer interval also to 30000 you'll see a very short flicker only once every 30 seconds. For my application this short flicker is acceptable. Btw, don't forget to turn on the timer! (At first I thought it didn't work, until I discovered I forgot to turn on the timer ;-))

要最小化重新激活工具提示时发生的闪烁,请将 InitialDelay 设置为 1。当您将 AutoPopDelay 设置为 30000 并且将计时器间隔也设置为 30000 时,您将看到每 30 秒一次的非常短的闪烁。对于我的应用程序,这种短暂的闪烁是可以接受的。顺便说一句,别忘了打开计时器!(起初我以为它不起作用,直到我发现我忘记打开计时器;-))

Of course this is a cure for the symptoms and not an elegant solution to the problem.

当然,这是对症状的治疗,而不是对问题的优雅解决方案。

回答by Jimi

As it has been posted countless times and is known from the .NET Framework early days, you cannot set the AutoPopDelay time higher than an Int16.MaxValue (i.e. 32767) and have it working. Using the tooltip Show() method leads to the same result. Any value higher than 32767 leads the timer to be reset to 5000 ms.

由于它已被无数次发布并且在 .NET Framework 早期就已经知道,因此您不能将 AutoPopDelay 时间设置为高于 Int16.MaxValue(即 32767)并使其工作。使用工具提示 Show() 方法会导致相同的结果。任何高于 32767 的值都会导致计时器重置为 5000 毫秒。

Even an attempt to set the AutomaticDelay to a value that leads to an AutoPopDelay value higher than 32767 will cause the tooltip not to work. Furthermore, using the StopTimer() method doesn't yield any positive results.

即使尝试将 AutomaticDelay 设置为导致 AutoPopDelay 值高于 32767 的值也会导致工具提示不起作用。此外,使用 StopTimer() 方法不会产生任何积极的结果。

The only way I was succesful in displaying a tooltip with infinite timeout was using the tooltip Show() method that passes the displayed position. tooltip.Show(text, control, position). In conclusion, the tooltip control has the most bizarre behavior I have yet encountered in a control and the documentation about setting the delay times is quite misleading.

我成功显示无限超时的工具提示的唯一方法是使用传递显示位置的工具提示 Show() 方法。tooltip.Show(text, control, position). 总之,工具提示控件具有我在控件中遇到的最奇怪的行为,并且有关设置延迟时间的文档非常具有误导性。