繁体   English   中英

WinForms 工具提示未显示

[英]WinForms tooltips not showing up

我有一个 WinForms 应用程序。 每个窗体和用户控件设置其工具提示如下:

// in the control constructor
var toolTip = new ToolTip();
this.Disposed += (o, e) => toolTip.Dispose();
toolTip.SetToolTip(this.someButton, "...");
toolTip.SetToolTip(this.someCheckBox, "...");
...

但是,当我将鼠标悬停在控件上时,工具提示不会出现。 这是使用工具提示的合适方式吗? 在应用程序的另一部分(例如,侦听某些事件)中是否会发生一些会阻止工具提示工作的事情?

请注意,我的外部窗体的工具条按钮(通过按钮的工具提示属性配置)上的工具提示按预期工作。

编辑:

我已经观察到更多,我注意到有时工具提示确实出现了,它只是非常“片状”。 基本上,有时当我将鼠标悬停在控件上时,它会非常短暂地显示然后闪烁。 我可以使用 .Show() 和长 AutoPopDelay 手动显示它,但它永远不会消失!

当我的工具提示没有出现在 RichTextBox 上大约 3-5 次时,我遇到了类似的问题。 即使使用toolTip.Show强制它显式显示也无济于事。 直到我改为由壳牌提到的-你告诉你希望你的提示出现:

'Dim pnt As Point
pnt = control.PointToClient(Cursor.Position)
pnt.X += 10 ' Give a little offset
pnt.Y += 10 ' so tooltip will look neat
toolTip.Show(text, control, pnt)

这样我的工具提示总是预期的时间地点出现。 祝你好运!

你的代码对我来说似乎没问题。 我在您的代码中找不到任何错误。 但是,只有在禁用控制时它才会失败。 顺便说一句,您可以尝试另一种方法。 但是,我不建议您像这样显示工具提示。

private void someButton_MouseEnter(...)
{
    toolTip.Show("Tooltip text goes here", (Button)sender);
}

您还可以指定应在.Show()方法中显示工具提示的位置。 您可以使用一些重载函数。 阅读msdn以获取有关ToolTip.Show()方法的更多信息。

我编写了以下方法将工具提示从父控件(具有工具提示集)“传播”到其子控件(除非它们有自己的覆盖工具提示)。

它旨在放入您开始使用的表单或控件中,但它也可以变成需要“父”参数的静态方法。

/// <summary>Setting a toolTip on a custom control unfortunately doesn't set it on child
/// controls within its drawing area. This is a workaround for that.</summary>
private void PropagateToolTips(Control parent = null)
{
    parent = parent ?? this;
    string toolTip = toolTip1.GetToolTip(parent);
    if (toolTip == "")
        toolTip = null;
    foreach (Control child in parent.Controls)
    {
        // If the parent has a toolTip, and the child control does not
        // have its own toolTip set - set it to match the parent toolTip.
        if (toolTip != null && String.IsNullOrEmpty(toolTip1.GetToolTip(child)))
            toolTip1.SetToolTip(child, toolTip);
        // Recurse on the child control
        PropagateToolTips(child);
    }
}

请注意,如果您使用多个ToolTip实例来管理父控件和子控件ToolTip ,则行为未定义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM