views:

819

answers:

3

I have some controls that I would like to display a ToolTip for when the mouse is hovering over it. How can I do this? I would like to know how to do this properly in code, but also in the designer (There is a ToolTip component in the toolbox, but I don't quite.. get it).

I wouldn't be surprised if this is a duplicate, but I can only find questions that are on more advanced, specific scenarios. I would like to know the basics.

+3  A: 

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");
}
Svetlozar Angelov
So I guess it doesn't matter if the `toolTip1` variable drops out of scope?
Svish
Well, it matters. It's better to make it class member, if you want to manage it from some other function..
Svetlozar Angelov
What about one static `ToolTip` for all your forms?
Svish
What do you mean by "static" tooltip. You can design a ToolTip class MyToolTip and use it wherever you want.
Svetlozar Angelov
It would be freed once it became unreferenced, which it wouldn't be, as the button and checkbox would still reference it
Rowland Shaw
+2  A: 

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
+1  A: 
  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
So you can use the same ToolTip for many controls with different texts?
Svish
@Svish: yes, that is the purpose of extender controls.
Fredrik Mörk
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
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
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