使用Windows窗体在按钮上显示工具提示

时间:2022-08-26 23:48:36

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

如何使用Windows窗体在按钮上显示工具提示?

8 个解决方案

#1


188  

The ToolTip is actually a WinForms control that handles displaying tool tips for multiple elements on a single form.

工具提示实际上是一个WinForms控件,用于在一个表单中为多个元素显示工具提示。

Say your button is called MyButton.

说你的按钮叫MyButton。

  1. Add a ToolTip control (under Common Controls in the Windows Forms toolbox) to your form.
  2. 在窗体上添加一个工具提示控件(在Windows窗体工具箱的常用控件下)。
  3. Give it a name - say MyToolTip
  4. 给它起个名字——比如MyToolTip
  5. Set the "Tooltip on MyToolTip" property of MyButton (under Misc in the button property grid) to the text that should appear when you hover over it.
  6. 将MyToolTip属性(在按钮属性网格中的Misc下)设置为鼠标悬停时应该出现的文本。

The tooltip will automatically appear when the cursor hovers over the button, but if you need to display it programatically, call

当光标悬停在按钮上时,工具提示将自动出现,但如果您需要以编程方式显示,则调用。

MyToolTip.Show("Tooltip text goes here", MyButton)

in your code to show the tooltip, and MyToolTip.Hide(MyButton) to make it disappear again.

在代码中显示工具提示,并隐藏(MyButton)使其再次消失。

#2


78  

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).
  • 找到“toolTip1上的工具提示”属性(如果您更改了它的默认名称,名称可能不是toolTip1)。
  • 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");

#3


31  

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

#4


4  

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

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

#5


3  

The .NET framework provides a ToolTip class. Add one of those to your form and then on the MouseHover event for each item you would like a tooltip for, do something like the following:

. net框架提供了一个工具提示类。将其中一个添加到窗体中,然后在鼠标悬停事件中为每个您想要的工具提示做如下操作:

    private void checkBox1_MouseHover(object sender, EventArgs e)
    {
        toolTip1.Show("text", checkBox1);
    }

#6


3  

Lazy and compact storing text in the Tag property

延迟和紧凑地存储标签属性中的文本

If you are a bit lazy and do not use the Tag property of the controls for anything else you can use it to store the tooltip text and assign MouseHover event handlers to all such controls in one go like this:

如果您有点懒惰,不使用控件的标记属性进行任何其他操作,您可以使用它来存储工具提示文本,并将鼠标悬停事件处理程序一次性分配给所有这些控件:

    private System.Windows.Forms.ToolTip ToolTip1;
    private void PrepareTooltips()
    {
        ToolTip1 = new System.Windows.Forms.ToolTip();
        foreach(Control ctrl in this.Controls)
        {
            if (ctrl is Button && ctrl.Tag is string)
            {
                ctrl.MouseHover += new EventHandler(delegate(Object o, EventArgs a)
                {
                    var btn = (Control)o;
                    ToolTip1.SetToolTip(btn, btn.Tag.ToString());
                });
            }
        }
    }

In this case all buttons having a string in the Tag property is assigned a MouseHover event. To keep it compact the MouseHover event is defined inline using a lambda expression. In the event any button hovered will have its Tag text assigned to the Tooltip and shown.

在这种情况下,标签属性中有字符串的所有按钮都被分配一个鼠标悬停事件。为了使其紧凑,鼠标悬停事件是使用lambda表达式内联定义的。在事件中,任何停留的按钮都将其标记文本分配给工具提示并显示出来。

#7


0  

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

#8


-4  

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:

当然,只需处理鼠标悬停事件并告诉它显示工具提示。t是一个工具提示,定义在全局或构造函数中使用:

ToolTip t = new ToolTip();

then the event handler:

然后事件处理程序:

private void control_MouseHover(object sender, EventArgs e)
{
  t.Show("Text", (Control)sender);
}

#1


188  

The ToolTip is actually a WinForms control that handles displaying tool tips for multiple elements on a single form.

工具提示实际上是一个WinForms控件,用于在一个表单中为多个元素显示工具提示。

Say your button is called MyButton.

说你的按钮叫MyButton。

  1. Add a ToolTip control (under Common Controls in the Windows Forms toolbox) to your form.
  2. 在窗体上添加一个工具提示控件(在Windows窗体工具箱的常用控件下)。
  3. Give it a name - say MyToolTip
  4. 给它起个名字——比如MyToolTip
  5. Set the "Tooltip on MyToolTip" property of MyButton (under Misc in the button property grid) to the text that should appear when you hover over it.
  6. 将MyToolTip属性(在按钮属性网格中的Misc下)设置为鼠标悬停时应该出现的文本。

The tooltip will automatically appear when the cursor hovers over the button, but if you need to display it programatically, call

当光标悬停在按钮上时,工具提示将自动出现,但如果您需要以编程方式显示,则调用。

MyToolTip.Show("Tooltip text goes here", MyButton)

in your code to show the tooltip, and MyToolTip.Hide(MyButton) to make it disappear again.

在代码中显示工具提示,并隐藏(MyButton)使其再次消失。

#2


78  

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).
  • 找到“toolTip1上的工具提示”属性(如果您更改了它的默认名称,名称可能不是toolTip1)。
  • 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");

#3


31  

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

#4


4  

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

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

#5


3  

The .NET framework provides a ToolTip class. Add one of those to your form and then on the MouseHover event for each item you would like a tooltip for, do something like the following:

. net框架提供了一个工具提示类。将其中一个添加到窗体中,然后在鼠标悬停事件中为每个您想要的工具提示做如下操作:

    private void checkBox1_MouseHover(object sender, EventArgs e)
    {
        toolTip1.Show("text", checkBox1);
    }

#6


3  

Lazy and compact storing text in the Tag property

延迟和紧凑地存储标签属性中的文本

If you are a bit lazy and do not use the Tag property of the controls for anything else you can use it to store the tooltip text and assign MouseHover event handlers to all such controls in one go like this:

如果您有点懒惰,不使用控件的标记属性进行任何其他操作,您可以使用它来存储工具提示文本,并将鼠标悬停事件处理程序一次性分配给所有这些控件:

    private System.Windows.Forms.ToolTip ToolTip1;
    private void PrepareTooltips()
    {
        ToolTip1 = new System.Windows.Forms.ToolTip();
        foreach(Control ctrl in this.Controls)
        {
            if (ctrl is Button && ctrl.Tag is string)
            {
                ctrl.MouseHover += new EventHandler(delegate(Object o, EventArgs a)
                {
                    var btn = (Control)o;
                    ToolTip1.SetToolTip(btn, btn.Tag.ToString());
                });
            }
        }
    }

In this case all buttons having a string in the Tag property is assigned a MouseHover event. To keep it compact the MouseHover event is defined inline using a lambda expression. In the event any button hovered will have its Tag text assigned to the Tooltip and shown.

在这种情况下,标签属性中有字符串的所有按钮都被分配一个鼠标悬停事件。为了使其紧凑,鼠标悬停事件是使用lambda表达式内联定义的。在事件中,任何停留的按钮都将其标记文本分配给工具提示并显示出来。

#7


0  

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

#8


-4  

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:

当然,只需处理鼠标悬停事件并告诉它显示工具提示。t是一个工具提示,定义在全局或构造函数中使用:

ToolTip t = new ToolTip();

then the event handler:

然后事件处理程序:

private void control_MouseHover(object sender, EventArgs e)
{
  t.Show("Text", (Control)sender);
}