Display a tooltip over a button using Windows Forms

后端 未结 9 1708
梦谈多话
梦谈多话 2020-11-29 17:37

How can I display a tooltip over a button using Windows Forms?

相关标签:
9条回答
  • 2020-11-29 18:00

    Using the form designer:

    • Drag the ToolTip control from the Toolbox, onto the form.
    • Select the properties of the control you want the tool tip to appear on.
    • Find the property 'ToolTip on toolTip1' (the name may not be toolTip1 if you changed it's default name).
    • Set the text of the property to the tool tip text you would like to display.

    You can set also the tool tip programatically using the following call:

    this.toolTip1.SetToolTip(this.targetControl, "My Tool Tip");
    
    0 讨论(0)
  • 2020-11-29 18:05

    For default tooltip this can be used -

    System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
    ToolTip1.SetToolTip(this.textBox1, "Hello world");
    

    A customized tooltip can also be used in case if formatting is required for tooltip message. This can be created by custom formatting the form and use it as tooltip dialog on mouse hover event of the control. Please check following link for more details -

    http://newapputil.blogspot.in/2015/08/create-custom-tooltip-dialog-from-form.html

    0 讨论(0)
  • 2020-11-29 18:07

    You can use the ToolTip class:

    Creating a ToolTip for a Control

    Example:

    private void Form1_Load(object sender, System.EventArgs e)
    {
        System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
        ToolTip1.SetToolTip(this.Button1, "Hello");
    }
    
    0 讨论(0)
  • 2020-11-29 18:11

    Sure, just handle the mousehover event and tell it to display a tool tip. t is a tooltip defined either in the globals or in the constructor using:

    ToolTip t = new ToolTip();
    

    then the event handler:

    private void control_MouseHover(object sender, EventArgs e)
    {
      t.Show("Text", (Control)sender);
    }
    
    0 讨论(0)
  • 2020-11-29 18:13
    private void Form1_Load(object sender, System.EventArgs e)
    {
        ToolTip toolTip1 = new ToolTip();
        toolTip1.AutoPopDelay = 5000;
        toolTip1.InitialDelay = 1000;
        toolTip1.ReshowDelay = 500;
        toolTip1.ShowAlways = true;
        toolTip1.SetToolTip(this.button1, "My button1");
        toolTip1.SetToolTip(this.checkBox1, "My checkBox1");
    }
    
    0 讨论(0)
  • 2020-11-29 18:17

    Based on DaveK's answer, I created a control extension:

    public static void SetToolTip(this Control control, string txt)
    {
        new ToolTip().SetToolTip(control, txt);
    }
    

    Then you can set the tooltip for any control with a single line:

    this.MyButton.SetToolTip("Hello world");
    
    0 讨论(0)
提交回复
热议问题