ToolTip.SetToolTip(Control, String) ToolTip.SetToolTip(Control, String) ToolTip.SetToolTip(Control, String) Method

定义

使工具提示文本与指定的控件相关联。Associates ToolTip text with the specified control.

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

参数

control
Control Control Control

要将工具提示文本与其关联的 ControlThe Control to associate the ToolTip text with.

caption
String String String

指针位于控件上方时要显示的工具提示文本。The ToolTip text to display when the pointer is on the control.

示例

下面的代码示例创建ToolTip类的一个实例, 并将该实例Form与在其中创建该实例的关联。The following code example creates an instance of the ToolTip class and associates the instance with the Form that the instance is created within. 然后, 该代码初始化延迟属性AutoPopDelayInitialDelayReshowDelayThe code then initializes the delay properties AutoPopDelay, InitialDelay, and ReshowDelay. 此外, ToolTip类的实例ShowAlways将属性设置为true , 以启用显示工具提示文本, 而不考虑窗体是否处于活动状态。In addition the instance of the ToolTip class sets the ShowAlways property to true to enable ToolTip text to be displayed regardless of whether the form is active. 最后, 该示例将工具提示文本与窗体上的Button两个控件 ( CheckBox和) 相关联。Finally, the example associates ToolTip text with two controls on a form, a Button and a CheckBox. 此示例要求在示例中定义的Form方法位于Button包含名为button1的控件和CheckBox名为checkBox1的控件, 并且该方法是从的构造函数中调用的FormThis example requires that the method defined in the example is located within a Form that contains a Button control named button1 and a CheckBox control named checkBox1, and that the method is called from the constructor of the 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

注解

除了指定要为控件显示的工具提示文本之外, 还可以使用此方法修改控件的工具提示文本。In addition to specifying the ToolTip text to display for a control, you can also use this method to modify the ToolTip text for a control. 为给定控件多次调用方法时,不会为控件指定要显示的多个工具提示文本,而是更改控件的当前工具提示文本。SetToolTipCalling the SetToolTip method more than once for a given control does not specify multiple ToolTip text to display for a control, but instead changes the current ToolTip text for the control. 若要确定在运行时与控件关联的工具提示文本, 请使用GetToolTip方法。To determine the ToolTip text that is associated with a control at run time, use the GetToolTip method.

一般原则是, 所使用的文本应简短;不过, 您可以使用\r\n转义符序列来插入换行符。As a general rule the text used should be short; however, you can insert line breaks by using the \r\n escape character sequence. 文本中的 "&" 符 (&) 按StripAmpersands属性描述的方式进行处理。Ampersands (&) in the text are handled as described by the StripAmpersands property.

适用于

另请参阅