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.
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.
Here is your article for doing it with code
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.
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-
Add a ToolTip object to your form. One object is enough for the entire form.
ToolTip toolTip = new ToolTip();
Add the control to the tooltip with the desired text.
toolTip.SetToolTip(Button1,"Click here");
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
).Just subscribe to the control's ToolTipTextNeeded event, and return e.TooltipText, much simpler.