📜  C#|工具提示类

📅  最后修改于: 2021-05-29 20:46:52             🧑  作者: Mango

在Windows窗体中,工具提示表示一个很小的弹出框,当您将指针或光标放在控件上时,该框就会出现,该控件的目的是提供有关Windows窗体中存在的控件的简短说明。 ToolTip类用于创建ToolTip控件,还提供不同类型的属性,方法,事件,还提供控件的运行时状态。
您可以在任何容器或控件中使用ToolTip类。在单个ToolTip组件的帮助下,您可以为多个控件创建多个工具提示。 System.Windows.Forms命名空间下定义的ToolTip类。在C#中,您可以使用两种不同的方式在Windows窗体中创建一个工具提示:

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

  • 第1步:创建一个Windows窗体,如下图所示:
    Visual Studio->文件->新建->项目-> WindowsFormApp
  • 步骤2:将工具提示从工具箱中拖放到窗体上。当您将此ToolTip拖放到窗体上时,它将自动添加到当前窗口中存在的每个控件的属性(在ToolTip1上命名为ToolTip)。
  • 步骤3:拖放之后,您将转到ToolTip控件的属性,以根据需要修改ToolTip。

    输出:

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

  • 步骤1:由ToolTip类提供使用ToolTip()构造函数创建ToolTip控件。
    // Creating a ToolTip control
    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");
        }
    }
    }
    

    输出:

建设者

Constructor Description
ToolTip() This Constructors is used to initialize a new instance of the ToolTip without a specified container.
ToolTip(IContainer) This Constructors is used to initialize a new instance of the ToolTip class with a specified container.

特性

Property Description
Active This property is used to get or set a value indicating whether the ToolTip is currently active.
AutomaticDelay This property is used to get or set the automatic delay for the ToolTip.
AutoPopDelay This property is used to get or set the period of time the ToolTip remains visible if the pointer is stationary on a control with specified ToolTip text.
BackColor This property is used to get or set the background color for the control.
ForeColor This property is used to get or set the foreground color of the control.
InitialDelay This property is used to get or set the time that passes before the ToolTip appears.
IsBalloon This property is used to get or set a value indicating whether the ToolTip should use a balloon window.
ReshowDelay This property is used to get or set the length of time that must transpire before subsequent ToolTip windows appear as the pointer moves from one control to another.
ToolTipIcon This property is used to get or set a value that defines the type of icon to be displayed alongside the ToolTip text.
ToolTipTitle This property is used to get or set a title for the ToolTip window.