C#中ToolTip控件应用实例
ToolTip控件
功能
该控件是一个长方形的弹出窗口,在用户将指针悬停在一个控件上时,该窗口显示有关该控件用途的简短说明。图1所示为ToolTip控件。

图1 ToolTip控件
2.属性
ToolTip控件常用属性及说明如表1所示。

表1 ToolTip控件常用属性及说明
下面详细介绍UseAnimation属性,此属性获取或设置一个值,该值确定在显示工具提示时,是否应使用动画效果。
语法:
本教程来自http://www.isstudy.com/
属性值:如果窗口使用动画效果,为True;否则为False。默认值为True。
3.方法
ToolTip控件常用方法及说明如表2所示。

表2 ToolTip控件常用方法及说明
下面详细介绍SetToolTip方法,此方法使工具提示文本与指定的控件相关联。
语法:
public void SetToolTip (Control control,string caption)
参数说明如下。
control:将工具提示文本与其关联的Control。
caption:指针位于控件上方时要显示的工具提示文本。
示例 网站源代码
SetToolTip方法的使用
本示例通过调用SetToolTip方法,设置一个【ToolTip】按钮相关联,示例运行结果如图2所示。

图2 SetToolTip方法的使用
程序主要代码如下:
toolTip1.SetToolTip(button1, "Button按钮");
完整程序代码如下:网站源代码
★★★★★主程序文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace _8_15
{
static class Program
{
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmToolTip());
}
}
}
★★★★★frmToolTip窗体设计文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace _8_15
{
public partial class frmToolTip : Form
{
public frmToolTip()
{
InitializeComponent();
}
private void frmToolTip_Load(object sender, EventArgs e)
{
toolTip1.SetToolTip(button2, "Button按钮");
toolTip1.UseAnimation = true;
toolTip1.Active = true;
}
private void toolTip1_Popup(object sender, PopupEventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
}
}
★★★★★frmToolTip窗体代码文件完整程序代码★★★★★
namespace _8_15
{
partial class frmToolTip
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// toolTip1
//
this.toolTip1.AutoPopDelay = 5000;
this.toolTip1.InitialDelay = 200;
this.toolTip1.ReshowDelay = 100;
this.toolTip1.ShowAlways = true;
this.toolTip1.Popup += new System.Windows.Forms.PopupEventHandler(this.toolTip1_Popup);
//
// button1
//
this.button1.Location = new System.Drawing.Point(71, 73);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "ToolTip";
this.button1.UseVisualStyleBackColor = true;
//
// button2
//
this.button2.Location = new System.Drawing.Point(93, 50);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 0;
this.button2.Text = "ToolTip";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// frmToolTip
//
this.ClientSize = new System.Drawing.Size(292, 142);
this.Controls.Add(this.button2);
this.Name = "frmToolTip";
this.Load += new System.EventHandler(this.frmToolTip_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ToolTip toolTip1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}