Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ToolTip.Show for an inactive window doesn't works

Why tooltip, displayed manually with ToolTip.Show, is not shown, when window, containing control, is inactive?

public class MyControl : Button
{

    private _tip;
    public string ToolTip
    {
        get { return _tip; }
        set { _tip = value; }
    }

    private ToolTip _toolTip = new ToolTip();

    public MyControl()
    {
        _toolTip.UseAnimation = false;
        _toolTip.UseFading = false;
        _toolTip.ShowAlways = true;
    }

    protected override void OnMouseHover(EventArgs e)
    {
        _toolTip.Show(_tip, this, 0, Height);
        base.OnMouseHover(e);
    }

    protected override void OnMouseLeave(EventArgs e)
    {
        _toolTip.Hide(this);
        base.OnMouseLeave(e);
    }
}

I went for ToolTip.Show because I must have tooltip onscreen for unlimited time, which is not possible with normal ToolTip. I also love the idea of having tooltip text as a part of control itself. But unfortunately, when showing tooltip this way for inactive window (despite ShowAlways = true), it simply doesn't work.

The OnMouseHower event is rised, but _toolTip.Show does nothing.. unless window is activated, then everything works.

Bounty

Adding bounty for a solution to display tooltip for an inactive form (preferably with solution when tooltip text is a property of control, not IContainer).

like image 512
Sinatr Avatar asked Oct 14 '13 15:10

Sinatr


1 Answers

There is a private method that does what you want, so to access it, you would have to use reflection to call it:

using System.Reflection;

public class MyControl : Button {
  private ToolTip toolTip = new ToolTip() {
    UseAnimation = false,
    UseFading = false
  };

  public string ToolTip { get; set; }

  protected override void OnMouseHover(EventArgs e) {
    base.OnMouseHover(e);
    Point mouse = MousePosition;
    mouse.Offset(10, 10);
    MethodInfo m = toolTip.GetType().GetMethod("SetTool",
                           BindingFlags.Instance | BindingFlags.NonPublic);
    m.Invoke(toolTip, new object[] { this, this.ToolTip, 2, mouse });
  }

  protected override void OnMouseLeave(EventArgs e) {
    base.OnMouseLeave(e);
    toolTip.Hide(this);
  }
}

The tip will display on an inactive window and it will stay on the screen indefinitely until the mouse moves off the control.

like image 182
LarsTech Avatar answered Sep 29 '22 05:09

LarsTech