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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/168550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:13:47  来源:igfitidea点击:

Display a tooltip over a button using Windows Forms

.netwinformsbuttontooltip

提问by Dylan Beattie

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

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

回答by Dylan Beattie

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

ToolTip 实际上是一个 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. Give it a name - say MyToolTip
  3. 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.
  1. 将 ToolTip 控件(在 Windows 窗体工具箱中的通用控件下)添加到您的窗体中。
  2. 给它起个名字 - 说 MyToolTip
  3. 将 MyButton 的“Tooltip on 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.

在您的代码中显示工具提示,并使用 MyToolTip.Hide(MyButton) 使其再次消失。

回答by jmatthias

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.
  • 将 ToolTip 控件从工具箱拖到窗体上。
  • 选择要显示工具提示的控件的属性。
  • 找到属性 'ToolTip on toolTip1'(如果您更改了默认名称,则名称可能不是 toolTip1)。
  • 将属性的文本设置为您要显示的工具提示文本。

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

您还可以使用以下调用以编程方式设置工具提示:

this.toolTip1.SetToolTip(this.targetControl, "My Tool Tip");

回答by DaveK

You can use the ToolTip class:

您可以使用 ToolTip 类:

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

回答by nvivekgoyal

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

回答by flodis

Lazy and compact storing text in the Tagproperty

Tag属性中惰性且紧凑地存储文本

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

如果您有点懒惰并且不将控件的Tag属性用于其他任何事情,您可以使用它来存储工具提示文本并一次性为所有此类控件分配MouseHover事件处理程序,如下所示:

    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 Tagproperty is assigned a MouseHoverevent. To keep it compact the MouseHoverevent is defined inline using a lambda expression. In the event any button hovered will have its Tagtext assigned to the Tooltip and shown.

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

回答by akn

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

回答by Timothy Carter

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 框架提供了一个 ToolTip 类。将其中一个添加到您的表单中,然后在您想要工具提示的每个项目的 MouseHover 事件上,执行以下操作:

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

回答by Fry

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:

当然,只需处理 mousehover 事件并告诉它显示工具提示。t 是在全局变量或构造函数中定义的工具提示,使用:

ToolTip t = new ToolTip();

then the event handler:

然后是事件处理程序:

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