176

I would like to display a ToolTip for when the mouse is hovering over a control.

How does one create a tooltip in code, but also in the designer?

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Svish
  • 152,914
  • 173
  • 462
  • 620
  • Possible duplicate of [Displaying tooltip on mouse hover of a text](http://stackoverflow.com/questions/873175/displaying-tooltip-on-mouse-hover-of-a-text) – Jim Fell Jun 10 '16 at 19:40
  • Related, and older still (applies to .NET in general): https://stackoverflow.com/questions/168550/display-a-tooltip-over-a-button-using-windows-forms – Knowledge Cube Jun 28 '17 at 17:31

6 Answers6

231

Here is your article for doing it with code

private void Form1_Load(object sender, System.EventArgs e)
{
     // Create the ToolTip and associate with the Form container.
     ToolTip toolTip1 = new ToolTip();

     // Set up the delays for the ToolTip.
     toolTip1.AutoPopDelay = 5000;
     toolTip1.InitialDelay = 1000;
     toolTip1.ReshowDelay = 500;
     // Force the ToolTip text to be displayed whether or not the form is active.
     toolTip1.ShowAlways = true;

     // Set up the ToolTip text for the Button and Checkbox.
     toolTip1.SetToolTip(this.button1, "My button1");
     toolTip1.SetToolTip(this.checkBox1, "My checkBox1");
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Svetlozar Angelov
  • 21,214
  • 6
  • 62
  • 67
  • 5
    So I guess it doesn't matter if the `toolTip1` variable drops out of scope? – Svish Aug 27 '09 at 08:06
  • Well, it matters. It's better to make it class member, if you want to manage it from some other function.. – Svetlozar Angelov Aug 27 '09 at 08:11
  • 2
    What about one static `ToolTip` for all your forms? – Svish Aug 27 '09 at 08:13
  • What do you mean by "static" tooltip. You can design a ToolTip class MyToolTip and use it wherever you want. – Svetlozar Angelov Aug 27 '09 at 08:23
  • 8
    It would be freed once it became unreferenced, which it wouldn't be, as the button and checkbox would still reference it – Rowland Shaw Aug 27 '09 at 09:04
  • @Rowland: The button and checkbox are not referencing it. – jnm2 Dec 31 '13 at 18:14
  • 2
    They are referencing it because "SetToolTip" adds EventHandlers to the button and checkbox's events – Eduardo Wada Jun 16 '14 at 19:46
  • 3
    I think its a bad idea to put the tooltip into a static variable. What if a Form is closed and opened again? Will the SetTooltip work again? And if so, will the handlers of the closed form stay in memory or removed correctly? I would not even spend one minute in testing that. Use a member variabe instead - thats bullet proof! – Elmue Jul 11 '14 at 05:19
  • One way to keep the tooltip from dropping out of scope is to assign it to the Tag property of the control. Then it stays until the control is destroyed. `this.button1.Tag = toolTip1` – Ed Williams Jan 17 '17 at 18:34
  • This looks incompatible with WinForms style form localization. Every other visible string can be set by ApplyResources, but this requires calling SetTooltip with another control as a key. Is there any option other than using non-form resources? – Paul Nov 28 '19 at 00:48
  • Don't instantiate the ToolTip like this. Open the form designer and drag one over. That way it gets disposed of with the rest of the form's controls. Then do the SetToolTip calls as shown above in your constructor, after the call to InitializeComponent(). – Charles Jenkins Jun 03 '20 at 18:06
156

Drag a tooltip control from the toolbox onto your form. You don't really need to give it any properties other than a name. Then, in the properties of the control you wish to have a tooltip on, look for a new property with the name of the tooltip control you just added. It will by default give you a tooltip when the cursor hovers the control.

JYelton
  • 35,664
  • 27
  • 132
  • 191
48
  1. Add a ToolTip component to your form
  2. Select one of the controls that you want a tool tip for
  3. Open the property grid (F4), in the list you will find a property called "ToolTip on toolTip1" (or something similar). Set the desired tooltip text on that property.
  4. Repeat 2-3 for the other controls
  5. Done.

The trick here is that the ToolTip control is an extender control, which means that it will extend the set of properties for other controls on the form. Behind the scenes this is achieved by generating code like in Svetlozar's answer. There are other controls working in the same manner (such as the HelpProvider).

Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
  • 1
    So you can use the same ToolTip for many controls with different texts? – Svish Aug 27 '09 at 08:05
  • @Svish: yes, that is the purpose of extender controls. – Fredrik Mörk Aug 27 '09 at 08:08
  • Can it be used cross usercontrols and forms too? Like if you created a static ToolTip with your standard property values. Or would that be considered bad practice? – Svish Aug 27 '09 at 08:10
  • I never tried it, and I don't quite know the inner workings of ToolTip, but since it gets a reference to the control to which a text is related I guess it *could* work. Only one way to find out; try it :o) – Fredrik Mörk Aug 27 '09 at 08:13
  • Well, I will obviously try it out. But I am not especially good at finding things like memory leaks and such... And if it would cause a memory leak or anything like that I would like to know, hehe. – Svish Aug 27 '09 at 08:16
  • Correct answer. – primo May 09 '23 at 05:05
8

ToolTip in C# is very easy to add to almost all UI controls. You don't need to add any MouseHover event for this.

This is how to do it-

  1. Add a ToolTip object to your form. One object is enough for the entire form. ToolTip toolTip = new ToolTip();

  2. Add the control to the tooltip with the desired text.

    toolTip.SetToolTip(Button1,"Click here");

OopsDev
  • 91
  • 1
  • 2
1

I did it this way: Just add the event to any control, set the control's tag, and add a conditional to handle the tooltip for the appropriate control/tag.

private void Info_MouseHover(object sender, EventArgs e)
{
    Control senderObject = sender as Control;
    string hoveredControl = senderObject.Tag.ToString();

    // only instantiate a tooltip if the control's tag contains data
    if (hoveredControl != "")
    {
        ToolTip info = new ToolTip
        {
            AutomaticDelay = 500
        };

        string tooltipMessage = string.Empty;

        // add all conditionals here to modify message based on the tag 
        // of the hovered control
        if (hoveredControl == "save button")
        {
            tooltipMessage = "This button will save stuff.";
        }

        info.SetToolTip(senderObject, tooltipMessage);
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
log-cab.in
  • 11
  • 3
-3

Just subscribe to the control's ToolTipTextNeeded event, and return e.TooltipText, much simpler.

fredv
  • 1
  • 2