在非聚焦的ToolStripItem上显示工具提示

fos*_*son 8 .net c# tooltip toolstrip winforms

当鼠标悬停在它们上时,ToolStripItems显示活动突出显示,即使它们所在的表单不在焦点上.但是,除非表格是重点,否则他们不会显示他们的工具提示.我见过ToolStrip'click-though'hack.任何人都知道如何使ToolStripButton在其父表单不在焦点时显示其工具提示?

谢谢!

fos*_*son 5

问题是像ToolStripButton或ToolStripDropDownButton这样的ToolStrip"控件"不会从Control继承.现在,只要用户将鼠标悬停在按钮上,就可以通过聚焦ToolStrip解决问题.按钮的MouseHover事件被触发太晚了 - 在运行"show tooltip"代码之后,我扩展了ToolStripDropDownButton类并使用了我的新按钮.此方法适用于从ToolStripItem继承的任何其他类按钮的类

public class ToolStripDropDownEx : ToolStripDropDownButton
{
    public ToolStripDropDownEx(string text)
    {
    }

    protected override void OnMouseHover(EventArgs e)
    {
        if (this.Parent != null)
            Parent.Focus();
        base.OnMouseHover(e);
    } 
}
Run Code Online (Sandbox Code Playgroud)


Mat*_*ley 3

也许这段代码中的两种方法之一会让您朝着正确的方向前进......

public Form1()
{
    InitializeComponent();

    tooltip = new ToolTip();
    tooltip.ShowAlways = true;
}

private ToolTip tooltip;
private void toolStripButton_MouseHover(object sender, EventArgs e)
{
    if (!this.Focused)
    {
        ToolStripItem tsi = (ToolStripItem)sender;
        tooltip.SetToolTip(toolStrip1, tsi.AutoToolTip ? tsi.ToolTipText : tsi.Text);
        /*tooltip.Show(tsi.AutoToolTip ? tsi.ToolTipText : tsi.Text, this, 
            new Point(toolStrip1.Left, toolStrip1.Bottom));*/
    }
}

private void toolStripButton_MouseLeave(object sender, EventArgs e)
{
    tooltip.RemoveAll();
}
Run Code Online (Sandbox Code Playgroud)

第一个的问题是你不能直接将它设置为按钮,它不继承自 Control,并且工具提示不会显示,除非你位于条带上而不是按钮上。

第二个(注释掉的方式)的问题是它根本不显示。不太清楚为什么,但也许你可以调试它。