C#: How do I add a ToolTip to a control?

2019-01-10 07:07发布

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.

6条回答
做自己的国王
2楼-- · 2019-01-10 07:37

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.

查看更多
地球回转人心会变
3楼-- · 2019-01-10 07:40

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");
}
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-01-10 07:41

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);
        }
    }
查看更多
够拽才男人
5楼-- · 2019-01-10 07:45

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");

查看更多
干净又极端
6楼-- · 2019-01-10 07: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).

查看更多
一夜七次
7楼-- · 2019-01-10 07:55

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

查看更多
登录 后发表回答