C#工具提示类用法介绍

2021年5月7日13:56:52 发表评论 787 次浏览

在Windows窗体中, 工具提示表示一个很小的弹出框, 当你将指针或光标放在控件上时会出现, 此控件的目的是提供有关Windows窗体中存在的控件的简短说明。 ToolTip类用于创建ToolTip控件, 还提供不同类型的属性, 方法, 事件, 还提供控件的运行时状态。

你可以在任何容器或控件中使用ToolTip类。在单个ToolTip组件的帮助下, 你可以为多个控件创建多个工具提示。在下面定义的ToolTip类

System.Windows.Forms

命名空间。在C#中, 你可以使用两种不同的方式在Windows窗体中创建一个工具提示:

1.设计时间:这是创建工具提示的最简单方法, 如以下步骤所示:

第1步:

创建一个Windows窗体, 如下图所示:

Visual Studio->文件->新建->项目-> WindowsFormApp

C#|工具提示类1

第2步:

将工具提示从工具箱中拖放到窗体上。当你将此ToolTip拖放到窗体上时, 它将自动添加到当前窗口中存在的每个控件的属性(在ToolTip1上命名为ToolTip)。

C#|工具提示类2

第三步:

拖放后, 你将转到ToolTip控件的属性, 以根据需要修改ToolTip。

C#|工具提示类3

输出如下:

C#|工具提示类4

2.运行时:它比上面的方法有些棘手。在此方法中, 可以借助ToolTip类提供的语法以编程方式创建ToolTip控件。以下步骤显示了如何动态设置创建工具提示:

步骤1:由ToolTip类提供使用ToolTip()构造函数创建ToolTip控件。 //创建ToolTip控件ToolTip t_Tip = new ToolTip();

第2步:

创建ToolTip控件后, 设置ToolTip类提供的ToolTip控件的属性。

//Seting the properties of ToolTip
t_Tip.Active = true; 
t_Tip.AutoPopDelay = 4000; 
t_Tip.InitialDelay = 600; 
t_Tip.IsBalloon = true; 
t_Tip.ToolTipIcon = ToolTipIcon.Info; 
t_Tip.SetToolTip(box1, "Name should start with Capital letter"); 
t_Tip.SetToolTip(box2, "Password should be greater than 8 words");

例子:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
  
namespace WindowsFormsApp34 {
  
public partial class Form1 : Form {
  
     public Form1()
     {
         InitializeComponent();
     }
  
     private void Form1_Load( object sender, EventArgs e)
     {
         //Creating and setting the
         //properties of the Label
         Label l1 = new Label();
         l1.Location = new Point(140, 122);
         l1.Text = "Name" ;
  
         //Adding this Label
         //control to the form
         this .Controls.Add(l1);
  
         //Creating and setting the
         //properties of the TextBox
         TextBox box1 = new TextBox();
         box1.Location = new Point(248, 119);
         box1.BorderStyle = BorderStyle.FixedSingle;
  
         //Adding this TextBox
         //control to the form
         this .Controls.Add(box1);
  
         //Creating and setting the
         //properties of Label
         Label l2 = new Label();
         l2.Location = new Point(140, 152);
         l2.Text = "Password" ;
  
         //Adding this Label
         //control to the form
         this .Controls.Add(l2);
  
         //Creating and setting the
         //properties of the TextBox
         TextBox box2 = new TextBox();
         box2.Location = new Point(248, 145);
         box2.BorderStyle = BorderStyle.FixedSingle;
  
         //Adding this TextBox
         //control to the form
         this .Controls.Add(box2);
  
         //Creating and setting the
         //properties of the ToolTip
         ToolTip t_Tip = new ToolTip();
         t_Tip.Active = true ;
         t_Tip.AutoPopDelay = 4000;
         t_Tip.InitialDelay = 600;
         t_Tip.IsBalloon = true ;
         t_Tip.ToolTipIcon = ToolTipIcon.Info;
         t_Tip.SetToolTip(box1, "Name should start with Capital letter" );
         t_Tip.SetToolTip(box2, "Password should be greater than 8 words" );
     }
}
}

输出如下:

C#|工具提示类5

建设者

建设者 描述
工具提示() 此构造函数用于在没有指定容器的情况下初始化ToolTip的新实例。
工具提示(IContainer) 此构造方法用于使用指定的容器初始化ToolTip类的新实例。

属性

属性 描述
活性 此属性用于获取或设置一个值, 该值指示工具提示当前是否处于活动状态。
自动延迟 此属性用于获取或设置工具提示的自动延迟。
AutoPopDelay 如果指针固定在具有指定ToolTip文本的控件上, 则此属性用于获取或设置ToolTip保持可见的时间。
背景色 此属性用于获取或设置控件的背景色。
前景色 此属性用于获取或设置控件的前景色。
初始延迟 此属性用于获取或设置工具提示出现之前经过的时间。
气球 此属性用于获取或设置一个值, 该值指示ToolTip是否应使用气球状窗口。
重新显示延迟 此属性用于获取或设置在指针从一个控件移动到另一个控件之前, 随后的工具提示窗口出现之前必须经过的时间。
工具提示图标 此属性用于获取或设置一个值, 该值定义要在工具提示文本旁边显示的图标的类型。
工具提示标题 此属性用于获取或设置"工具提示"窗口的标题。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: