📜  如何在C#中的工具提示中设置图标?

📅  最后修改于: 2021-05-29 23:22:07             🧑  作者: Mango

在Windows窗体中,工具提示表示一个很小的弹出框,当您将指针或光标放在控件上时,该框就会出现,该控件的目的是提供有关Windows窗体中存在的控件的简短说明。在ToolTip中,可以使用ToolTipIcon属性在ToolTip窗口中使用ToolTip文本设置图标。此属性接受在ToolTipIcon枚举下定义的四种不同类型的值,这些值是:

  • 无:表示“工具提示”窗口不包含图标。
  • 信息:这是一个信息图标。
  • 警告:这是一个警告图标。
  • 错误:这是一个错误图标。

您可以通过两种不同的方式设置此属性:

1.设计时:这是设置ToolTipIcon属性值的最简单方法,如以下步骤所示:

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

    输出:

2.运行时:比上述方法有些棘手。在此方法中,可以借助给定的语法以编程方式设置ToolTip的ToolTipIcon属性:

public System.Windows.Forms.ToolTipIcon ToolTipIcon { get; set; }

在这里,ToolTipIcon表示由ToolTipIcon枚举提供的值。以下步骤显示如何动态设置ToolTip的ToolTipIcon属性:

  • 第1步:使用ToolTip类提供的ToolTip()构造函数创建ToolTip。
    // Creating a ToolTip
    ToolTip t = new ToolTip();
    
  • 步骤2:创建工具提示后,设置由工具提示类提供的工具提示的ToolTipIcon属性。
    // Setting the ToolTipIcon property
    t.ToolTipIcon = ToolTipIcon.Info;
    
  • 步骤3:最后使用SetToolTip()方法将此ToolTip添加到控件中。此方法包含控件名称和要在“工具提示”框中显示的文本。
    t.SetToolTip(box1, "Name should start with Capital letter");

    例子:

    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 = new ToolTip();
            t.Active = true;
            t.AutoPopDelay = 4000;
            t.InitialDelay = 600;
            t.IsBalloon = true;
            t.ToolTipIcon = ToolTipIcon.Info;
            t.SetToolTip(box1, "Name should start with Capital letter");
            t.SetToolTip(box2, "Password should be greater than 8 words");
        }
    }
    }
    

    输出: