语言

ToolTip.SetToolTip(Control, String) 方法

定义

将工具提示文本与指定的控件相关联。

public:
 void SetToolTip(System::Windows::Forms::Control ^ control, System::String ^ caption);
public void SetToolTip(System.Windows.Forms.Control control, string caption);
public void SetToolTip(System.Windows.Forms.Control control, string? caption);
member this.SetToolTip : System.Windows.Forms.Control * string -> unit
Public Sub SetToolTip (control As Control, caption As String)

参数

control
Control

要 Control 与工具提示文本相关联。

caption
String

当指针位于控件上时要显示的工具提示文本。

示例

下面的代码示例创建类的 ToolTip 一个实例,并将该实例与 Form 在其中创建的实例相关联。 然后,代码初始化延迟属性 AutoPopDelay, InitialDelay以及 ReshowDelay。 此外,类的 ToolTip 实例设置 ShowAlways 属性以 true 启用要显示的工具提示文本,而不管窗体是否处于活动状态。 最后,该示例将工具提示文本与窗体、a Button 和 a 上的两个 CheckBox控件相关联。 此示例要求在示例中定义的方法位于一个包含Form命名控件Button和button1命名CheckBox控件的控件中checkBox1,并且该方法是从构造函数调用的Form。

// 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.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 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

注解

除了指定要为控件显示的工具提示文本之外,还可以使用此方法修改控件的工具提示文本。 SetToolTip为给定控件多次调用该方法不会指定要为控件显示的多个工具提示文本,而是更改控件的当前工具提示文本。 若要确定运行时与控件关联的工具提示文本,请使用 GetToolTip 该方法。

一般情况下,使用的文本应短;但是,可以使用转义字符序列插入换行符 \r\n 。 文本中的 Ampersands (>)按属性描述 StripAmpersands 进行处理。

适用于

另请参阅