簡體   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