How do I add a ToolTip to a control?

前端 未结 6 1412
时光说笑
时光说笑 2020-11-30 20:50

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 prope

相关标签:
6条回答
  • 2020-11-30 20:54

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

    0 讨论(0)
  • 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);
        }
    }
    
    0 讨论(0)
  • 2020-11-30 21:08

    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");
    }
    
    0 讨论(0)
  • 2020-11-30 21:13

    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.

    0 讨论(0)
  • 2020-11-30 21:17
    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).

    0 讨论(0)
  • 2020-11-30 21:18

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

    0 讨论(0)
提交回复
热议问题