1

I have a Tooltip with the ShowAlways property set to true.

On the controls where I want the tooltip to display (LinkLabels in this instance), I see there is a "ToolTip on <name of my Tooltip>" property which expects a string.

However, my tooltip is shared between 5 LinkLabels, and should differ depending on which one is hovered over.

I do have a shared click event that works:

private void linkLabelPlatypus1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    int Platypus = 1;
    LinkLabel ll = null;
    if (sender is LinkLabel)
    {
        ll = sender as LinkLabel;
    }
    if (null != ll)
    {
        if (ll.Name == linkLabelPlatypus2.Name)
        {
            Platypus = 2;
        } else if (ll.Name == linkLabelPlatypus3.Name)
        {
            Platypus = 3;
        } else if (ll.Name == linkLabelPlatypus4.Name)
        {
            Platypus = 4;
        } else if (ll.Name == linkLabelPlatypus5.Name)
        {
            Platypus = 5;
        }
        toolTipPlatypi.SetToolTip(ll, DuckbillData.GetPlatypusDataForToolTip(Platypus)); 
    }
}

...but I want the tooltips to also show on hover, and not require the user to click the label.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

2 Answers2

4

You only need to set the tooltip once :

public Form1()
{
    InitializeComponent();

    toolTip1.SetToolTip(linkLabel1, "foo");
    toolTip1.SetToolTip(linkLabel2, "bar");
}

Done.

Doing this in a MouseHover or MouseEnter handler will call this function over and over each time the event fires. It will work, but it is unnecessarily complicated.

You only need one ToolTip on a form to provide tips for any number of components and it can provide them all simultaneously and continuously (ie: you don't have to change it or set it each time). Each component can have only one tip, but you can change it throughout the program any time you like. ShowAlways does not have to be true - it is used to make tooltips show on forms which are not active (ie: hover over an inactive window behind one with focus, etc).

J...
  • 30,968
  • 6
  • 66
  • 143
  • But if I set them all from the git-go, it would mean four calls to the database, and it's possible the user will never hover, so I think the JIT setting of the tooltip is preferable (in this case; but that's a good tip - no pun intended - for other usages of tooltips. – B. Clay Shannon-B. Crow Raven Sep 28 '12 at 18:09
  • @ClayShannon - if you are getting your tips from a database (ie: `GetPlatypusDataForToolTip` is a function that queries a DB) then it is even *more* of a reason not to put it in a hover or enter handler - that would generate DB calls every time the mouse crossed over or hovered over the label! If you want to do it JIT then at least set up some flags to make sure that you only do it the first time - once the tooltip is set it does not need to be done again. – J... Sep 28 '12 at 18:13
2

You should write a event handler for Mouse Hover and have your tool tip display logic inside it.

    private void Label1_MouseHover(object sender, System.EventArgs e) 
    {
       //display logic
    }

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousehover.aspx

Prabhu Murthy
  • 9,031
  • 5
  • 29
  • 36