ToolTip 类

表示一个长方形的小弹出窗口,该窗口在用户将指针悬停在一个控件上时显示有关该控件用途的简短说明。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Class ToolTip
    Inherits Component
    Implements IExtenderProvider
用法
Dim instance As ToolTip
public class ToolTip : Component, IExtenderProvider
public ref class ToolTip : public Component, IExtenderProvider
public class ToolTip extends Component implements IExtenderProvider
public class ToolTip extends Component implements IExtenderProvider

备注

使用 ToolTip 类,您可在用户将指针放置在控件上时为用户显示提示信息。ToolTip 类通常用来向用户提示控件的预期用途。例如,可以为接受名称的 TextBox 控件指定工具提示文本,同时指定要键入到控件中的名称的格式。除了提供提示外,还可使用 ToolTip 类提供运行时状态信息。例如,当用户将指针移动到显示 Internet 连接状态的 PictureBox 控件上时,可以使用 ToolTip 类显示连接速度和线路质量数据。

ToolTip 类可在任何容器内使用。若要显式指定容器,请使用 ToolTip(IContainer) 构造函数。单个 ToolTip 组件通常用于为单个窗体上的多个控件创建工具提示。在创建 ToolTip 之后,使用不同的 SetToolTip 方法调用可将工具提示显示文本与每个控件关联。然后,当用户将指针移到控件上时,即会显示工具提示及其文本。对于同一个控件可多次调用 SetToolTip 以更改与控件关联的文本。若要获取与控件关联的文本,请使用 GetToolTip 方法。若要移除 ToolTip 类的某个实例的所有工具提示文本关联,请使用 RemoveAll 方法。

提示

不会为禁用的控件显示工具提示文本。除非 ShowAlways 属性设置为 true,否则工具提示在其容器处于非活动状态时不会显示。

ToolTip 类提供了以下属性和方法以修改工具提示的默认行为和外观。

类别

关联成员

手动显示

Active, Show, Hide, ShowAlways, Popup, StopTimer

工具提示计时

AutoPopDelay, InitialDelay, ReshowDelay, AutomaticDelay, StopTimer

内容

SetToolTip, GetToolTip, StripAmpersands, ToolTipIcon, ToolTipTitle, RemoveAll

外观

BackColor, ForeColor, IsBalloon, OwnerDraw, UseAnimation, UseFading

如果要禁用所有工具提示文本以便不在应用程序中显示它,可以使用 Active 属性。工具提示通常由操作系统绘制,但若要自定义 ToolTip 的外观,可将 OwnerDraw 属性设置为 true 并处理 Draw 事件。

ToolTipTitle 类实现 System.ComponentModel.IExtenderProvider 接口,该接口只有一个 CanExtend 方法。工具提示在设计时扩展同一窗体上的控件,添加一个 ToolTip 属性。有关扩展程序提供程序的更多信息,请参见 扩展程序提供程序

示例

下面的代码示例创建一个 ToolTip 类的实例,并将该实例与创建它时所在的 Form 相关联。代码随后初始化延迟属性 AutoPopDelayInitialDelayReshowDelay。另外,ToolTip 类的实例将 ShowAlways 属性设置为 true,以使工具提示文本始终显示而不管窗体是否活动。最后,该示例将工具提示文本与窗体上的 ButtonCheckBox 这两个控件关联。本代码示例要求示例中定义的方法位于 Form 之内,而该窗体应包含一个名为 button1Button 控件和一个名为 checkBox1CheckBox 控件,并且要求从 Form 的构造函数中调用该方法。

' This example assumes that the Form_Load event handling method
' is connected to the Load event of the form.
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
   ' Create the ToolTip and associate with the Form container.
   Dim toolTip1 As 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(Me.button1, "My button1")
   toolTip1.SetToolTip(Me.checkBox1, "My checkBox1")
End Sub
// This example assumes that the Form_Load event handling method
// is connected to the Load event of the form.
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");
}
// This example assumes that the Form_Load event handling method
// is connected to the Load event of the form.
void Form1_Load( Object^ sender, System::EventArgs^ e )
{
   // Create the ToolTip and associate with the Form container.
   ToolTip^ toolTip1 = gcnew 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" );
}
// This example assumes that the Form_Load event handling method
// is connected to the Load event of the form.
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.set_AutoPopDelay(5000);
    toolTip1.set_InitialDelay(1000);
    toolTip1.set_ReshowDelay(500);
    // Force the ToolTip text to be displayed whether or not the form
    // is active.
    toolTip1.set_ShowAlways(true);
    // Set up the ToolTip text for the Button and Checkbox.
    toolTip1.SetToolTip(this.button1, "My button1");
    toolTip1.SetToolTip(this.checkBox1, "My checkBox1");
} //Form1_Load

继承层次结构

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
      System.Windows.Forms.ToolTip

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

ToolTip 成员
System.Windows.Forms 命名空间
ToolTipIcon
HelpProvider 类

其他资源

扩展程序提供程序
ToolTip 组件(Windows 窗体)