94

It seems that the Label has no Hint or ToolTip or Hovertext property. So what is the preferred method to show a hint, tooltip, or hover text when the Label is approached by the mouse?

Yuck
  • 49,664
  • 13
  • 105
  • 135
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 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

6 Answers6

129

You have to add a ToolTip control to your form first. Then you can set the text it should display for other controls.

Here's a screenshot showing the designer after adding a ToolTip control which is named toolTip1:

enter image description here

Yuck
  • 49,664
  • 13
  • 105
  • 135
  • 27
    Wow, that seems convoluted/counterintuitive, Yuck. – B. Clay Shannon-B. Crow Raven Mar 19 '12 at 19:29
  • 1
    @ClayShannon In a way I suppose it is. But the design is somewhat elegant. Some controls will never want tool tips. This way, the `ToolTip` control can just register itself for mouse over events and display the proper text depending on the event raised. This all happens in the background. – Yuck Mar 19 '12 at 19:37
  • 2
    I agree. It also allows you to use the same tooltip control for multiple controls. – Mark Ainsworth Jul 12 '15 at 18:55
  • @MarkAinsworth since one comment says it's good and one that it's bad, perhaps you state whether you agree it's good or agree it's bad? . I suppose you're saying you agree it's good. – barlop Feb 20 '18 at 12:39
  • I think this is a bad design because it only support a static tooltip. How would you update the tooltip at runtime? – Arvo Bowen Nov 30 '18 at 15:41
  • @ArvoBowen You'd access the `ToolTip` object and change its value there. This can be done at runtime pretty easily. – Yuck Dec 02 '18 at 20:47
  • Ahh I see, the answer was not 100% clear for me when I was first looking at it. Thanks – Arvo Bowen Dec 03 '18 at 00:10
  • I Think it worth to say that to add a tooltip you shoul toolTip1.SetToolTip(Control, "tooltip text here"); – Daniel May 09 '19 at 15:37
98
yourToolTip = new ToolTip();
//The below are optional, of course,

yourToolTip.ToolTipIcon = ToolTipIcon.Info;
yourToolTip.IsBalloon = true;
yourToolTip.ShowAlways = true;

yourToolTip.SetToolTip(lblYourLabel,"Oooh, you put your mouse over me.");
SeeSharp
  • 2,760
  • 16
  • 15
  • 1
    Don't forget to dispose the tooltip if you do this a lot on each mouseover, you'll leak handles until the GC calls the finalizer on the older tooltips. – Drakarah Dec 30 '13 at 10:34
26
System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
ToolTip1.SetToolTip( Label1, "Label for Label1");
scibuff
  • 13,377
  • 2
  • 27
  • 30
18

just another way to do it.

Label lbl = new Label();
new ToolTip().SetToolTip(lbl, "tooltip text here");
ac0de
  • 260
  • 2
  • 10
5

Just to share my idea...

I created a custom class to inherit the Label class. I added a private variable assigned as a Tooltip class and a public property, TooltipText. Then, gave it a MouseEnter delegate method. This is an easy way to work with multiple Label controls and not have to worry about assigning your Tooltip control for each Label control.

    public partial class ucLabel : Label
    {
        private ToolTip _tt = new ToolTip();

        public string TooltipText { get; set; }

        public ucLabel() : base() {
            _tt.AutoPopDelay = 1500;
            _tt.InitialDelay = 400;
//            _tt.IsBalloon = true;
            _tt.UseAnimation = true;
            _tt.UseFading = true;
            _tt.Active = true;
            this.MouseEnter += new EventHandler(this.ucLabel_MouseEnter);
        }

        private void ucLabel_MouseEnter(object sender, EventArgs ea)
        {
            if (!string.IsNullOrEmpty(this.TooltipText))
            {
                _tt.SetToolTip(this, this.TooltipText);
                _tt.Show(this.TooltipText, this.Parent);
            }
        }
    }

In the form or user control's InitializeComponent method (the Designer code), reassign your Label control to the custom class:

this.lblMyLabel = new ucLabel();

Also, change the private variable reference in the Designer code:

private ucLabel lblMyLabel;
cChacon
  • 184
  • 1
  • 8
3

I made a helper to make life easier.

public static class ControlUtilities1
{
    public static Control AddToolTip(this Control control, string title, string text)
    {
        var toolTip = new ToolTip
        {
            ToolTipIcon = ToolTipIcon.None,
            IsBalloon = true,
            ShowAlways = true,
            ToolTipTitle = title,
        };
        toolTip.SetToolTip(control, text);
        return control;
    }
}

Call it after controls are ready:

        InitializeComponent();
        ...
        linkLabelChiValues.AddToolTip(title, text);

It's an way to keep consistent tool tip styles.

cheny
  • 2,545
  • 1
  • 24
  • 30